Basic Syntax of C Language

We typically divide the basic syntax of the C language into several parts: data types, operators, control statements, functions, arrays, pointers, structures, input and output, etc. Below is an overview of these basic syntaxes.

1. Data Types:

  • Basic types: integer types (int, short, long, char), floating-point types (float, double)

  • Enumeration type (enum)

  • void type

  • Derived types: pointer types, array types, structure types (struct), union types (union)

2. Variables and Constants:

  • Variable declaration: data type variable name;

  • Constants: use the const keyword, or define using #define preprocessor directive.

3. Operators:

  • Arithmetic operators: +, -, *, /, %

  • Relational operators: ==, !=, >, <, >=, <=

  • Logical operators: &&, ||, !

  • Bitwise operators: &, |, ^, ~, <<, >>

  • Assignment operators: =, +=, -=, *=, /=, etc.

  • Other operators: ternary operator (?:), sizeof, & (address of), * (dereference)

4. Control Statements:

  • Conditional statements: if, if-else, switch

  • Loop statements: for, while, do-while

  • Jump statements: break, continue, goto, return

5. Functions:

  • Function definition: return type function name(parameter list) { function body }

  • Function declaration: informs the compiler of the function’s name, return type, and parameter list.

  • Function call: function name(parameter list);

6. Arrays:

  • One-dimensional array: type array name[size];

  • Multi-dimensional array: type array name[size1][size2]…;

7. Pointers:

  • Pointer variable: type *pointer name;

  • Pointer operations: address of (&), dereference (*), pointer arithmetic (e.g., incrementing a pointer to point to the next element)

8. Structures:

  • Defining a structure: struct structure name { member list };

  • Declaring a structure variable: struct structure name variable name;

  • Accessing members: use the dot operator (.) or arrow operator (->) (for structure pointers)

9. Input and Output:

  • Standard input/output functions: printf, scanf, getchar, putchar, etc.

  • File input/output: fopen, fclose, fread, fwrite, etc.

10. Preprocessor:

  • File inclusion: #include

  • Macro definition: #define

  • Conditional compilation: #if, #ifdef, #ifndef, #else, #elif, #endif

11. Memory Management:

  • Dynamic memory allocation: malloc, calloc, realloc, free

Important Notes

  • Statement termination: each statement ends with a semicolon ;

  • Code blocks: use curly braces {} to define code blocks

  • Case sensitivity: C language is case-sensitive

  • Variable scope: variables are only valid within the code block where they are defined

  • Header files: use #include to include necessary header files

Leave a Comment