Basic syntax and conventions of C

The C programming language is known for its simplicity and efficiency. Understanding its basic syntax and conventions is essential for writing effective C programs. Here are the key aspects:

1. Basic Structure of a C Program

A C program typically includes the following components:

  • Preprocessor directives
  • main function
  • Variable declarations
  • Statements and expressions
  • Functions
#include <stdio.h>  // Preprocessor directive

int main() {  // main function
    // Variable declaration
    int number;

    // Statements and expressions
    number = 10;
    printf("The number is %d\n", number);

    // Return statement
    return 0;
}

2. Preprocessor Directives

Preprocessor directives are commands that are processed before the compilation of the program. They typically begin with #.

#include <stdio.h>  // Includes the standard input-output library
#define PI 3.14  // Defines a constant

3. Comments

Comments are used to explain code and are ignored by the compiler. C supports both single-line and multi-line comments.

// This is a single-line comment

/*
 This is a
 multi-line comment
*/

4. Data Types

C provides several basic data types.

int number = 5;        // Integer
float price = 9.99;    // Floating-point number
char letter = 'A';     // Character
double pi = 3.14159;   // Double-precision floating-point number

5. Variables

Variables must be declared before they are used.

int age;
float salary;
char initial;

6. Input and Output

The printf and scanf functions are used for output and input, respectively.

#include <stdio.h>

int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);
    printf("You entered: %d\n", number);
    return 0;
}

7. Operators

C supports a variety of operators, including arithmetic, relational, logical, bitwise, and assignment operators.

int a = 5, b = 10;
int sum = a + b;       // Arithmetic operator
int is_equal = (a == b); // Relational operator
int and_result = (a && b); // Logical operator
int or_result = (a | b);   // Bitwise operator

8. Control Structures

C provides control structures such as if, else, while, for, and switch for flow control.

// if-else
if (a > b) {
    printf("a is greater than b\n");
} else {
    printf("a is not greater than b\n");
}

// for loop
for (int i = 0; i < 10; i++) {
    printf("%d\n", i);
}

// while loop
int count = 0;
while (count < 10) {
    printf("%d\n", count);
    count++;
}

// switch statement
switch (a) {
    case 1:
        printf("a is 1\n");
        break;
    case 2:
        printf("a is 2\n");
        break;
    default:
        printf("a is not 1 or 2\n");
        break;
}

9. Functions

Functions in C are used to encapsulate code into reusable blocks.

#include <stdio.h>

// Function declaration
void greet();

// Main function
int main() {
    greet();  // Function call
    return 0;
}

// Function definition
void greet() {
    printf("Hello, World!\n");
}

10. Arrays and Strings

Arrays are used to store multiple values of the same type. Strings are arrays of characters.

int numbers[5] = {1, 2, 3, 4, 5};  // Array of integers
char name[] = "John";  // String

// Accessing array elements
for (int i = 0; i < 5; i++) {
    printf("%d\n", numbers[i]);
}

11. Pointers

Pointers store the address of variables and are a powerful feature in C.

int x = 10;
int *p = &x;  // Pointer to x
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", p);
printf("Value pointed to by p: %d\n", *p);

12. Structs

Structs are user-defined data types that group related variables.

struct Person {
    char name[50];
    int age;
    float salary;
};

int main() {
    struct Person person1;
    person1.age = 30;
    person1.salary = 50000;
    strcpy(person1.name, "Alice");

    printf("Name: %s\n", person1.name);
    printf("Age: %d\n", person1.age);
    printf("Salary: %.2f\n", person1.salary);

    return 0;
}

13. Memory Management

C provides malloc, calloc, realloc, and free for dynamic memory allocation and deallocation.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(5 * sizeof(int));  // Allocating memory
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Using allocated memory
    for (int i = 0; i < 5; i++) {
        ptr[i] = i + 1;
    }

    // Freeing allocated memory
    free(ptr);

    return 0;
}

Understanding these basic syntax and conventions is crucial for writing and understanding C programs effectively.

Leave a Comment