C Basic Syntax
This chapter introduces the basic syntax rules of C: identifiers, keywords, data types, statements, expressions, comments, code style, etc.
1. Identifiers and Naming Conventions
- Composed of letters, numbers, underscores, cannot start with a number
- Case sensitive
- Recommended to use meaningful lowercase with underscores (snake_case)
2. Keywords
Examples: int, char, float, double, if, else, switch, for, while, do, return, break, continue, sizeof, typedef, struct, union, enum, const, static, volatile, etc.
3. Statements and Semicolons
Each statement ends with a semicolon ;, code blocks are wrapped with curly braces {}.
4. Variables and Constants
c
const int MAX = 100; // constant
int a = 10; // variable5. Expressions and Operators
c
int x = 3 + 4 * 2; // operator precedence: multiplication before addition
x += 5; // compound assignment
int y = (x > 10) ? 1 : 0; // conditional operator6. Input and Output
c
#include <stdio.h>
int a; scanf("%d", &a);
printf("a=%d\n", a);Common format specifiers: %d, %u, %ld, %f, %lf, %c, %s, %p.
7. Control Structures
- Conditional: if/else, switch
- Loops: for, while, do-while
- Jumps: break, continue, return
8. Functions
c
int add(int a, int b) { return a + b; }
int main(void) { printf("%d\n", add(2,3)); return 0; }9. Header Files and Preprocessing
c
#include <stdio.h>