C语言语法基础 #
一、程序基本结构 #
1.1 最简程序 #
c
int main(void) {
return 0;
}
1.2 完整程序结构 #
c
#include <stdio.h>
#define PI 3.14159
int global_var = 10;
int add(int a, int b);
int main(void) {
int local_var = 20;
printf("Hello, World!\n");
return 0;
}
int add(int a, int b) {
return a + b;
}
1.3 程序组成部分 #
| 组成部分 | 说明 |
|---|---|
| 预处理指令 | #include、#define等 |
| 全局声明 | 全局变量、函数声明 |
| main函数 | 程序入口 |
| 函数定义 | 自定义函数 |
二、注释 #
2.1 单行注释 #
c
int a = 10;
从 // 到行末都是注释。
2.2 多行注释 #
c
/*
* 这是一个多行注释
* 可以跨越多行
*/
int main() {
return 0;
}
从 /* 到 */ 之间的内容都是注释。
2.3 注释不能嵌套 #
c
/*
外层注释
/*
内层注释
*/
*/
这是错误的,注释不能嵌套。
2.4 注释使用场景 #
解释代码:
c
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
is_leap = 1;
}
临时禁用代码:
c
int main() {
printf("调试信息\n");
return 0;
}
文档注释:
c
/**
* @brief 计算阶乘
* @param n 非负整数
* @return n的阶乘
*/
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
三、标识符 #
3.1 标识符规则 #
标识符用于命名变量、函数、数组等。
规则:
- 只能由字母、数字、下划线组成
- 必须以字母或下划线开头
- 不能使用关键字
- 区分大小写
合法标识符:
c
int age;
int _count;
int student_name;
int MAX_SIZE;
int value1;
非法标识符:
c
int 2nd_place;
int my-var;
int int;
int @email;
3.2 命名规范 #
驼峰命名法:
c
int studentAge;
void calculateTotal();
下划线命名法:
c
int student_age;
void calculate_total();
常量命名:
c
#define MAX_SIZE 100
#define PI 3.14159
类型命名:
c
typedef struct {
int x;
int y;
} Point;
3.3 命名建议 #
| 类型 | 命名风格 | 示例 |
|---|---|---|
| 变量 | 小写+下划线 | student_name |
| 函数 | 小写+下划线 | calculate_sum |
| 常量 | 大写+下划线 | MAX_BUFFER_SIZE |
| 类型 | 首字母大写 | StudentInfo |
| 宏 | 大写+下划线 | MIN(a, b) |
四、关键字 #
4.1 C语言关键字列表 #
C语言共有32个关键字:
| 类型 | 关键字 |
|---|---|
| 数据类型 | int, char, float, double, void |
| 类型修饰 | short, long, signed, unsigned |
| 存储类 | auto, static, extern, register |
| 控制语句 | if, else, switch, case, default |
| 循环语句 | for, while, do |
| 跳转语句 | break, continue, return, goto |
| 自定义类型 | struct, union, enum |
| 其他 | const, volatile, sizeof, typedef |
4.2 关键字分类 #
数据类型关键字:
c
int a = 10;
char c = 'A';
float f = 3.14;
double d = 3.14159;
void func();
控制语句关键字:
c
if (a > 0) {
} else {
}
switch (choice) {
case 1: break;
default: break;
}
for (int i = 0; i < 10; i++) {
}
while (condition) {
}
do {
} while (condition);
存储类关键字:
c
auto int a;
static int count = 0;
extern int global_var;
register int fast_var;
其他关键字:
c
const int MAX = 100;
volatile int flag;
int size = sizeof(int);
typedef int Integer;
4.3 C99新增关键字 #
c
inline int add(int a, int b) {
return a + b;
}
_Bool flag = 1;
_Complex z;
_Imaginary im;
4.4 C11新增关键字 #
c
_Atomic int counter;
_Thread_local int tls_var;
_Alignas(16) int aligned_var;
_Alignof(int)
_Static_assert(sizeof(int) == 4, "int must be 4 bytes");
五、语句 #
5.1 语句类型 #
表达式语句:
c
a = 10;
b = a + 5;
func();
复合语句:
c
{
int a = 10;
int b = 20;
printf("%d\n", a + b);
}
控制语句:
c
if (a > 0) {
printf("正数\n");
}
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
空语句:
c
;
5.2 语句块 #
用 {} 括起来的语句序列:
c
{
int a = 10;
int b = 20;
int sum = a + b;
printf("sum = %d\n", sum);
}
语句块创建新的作用域:
c
int a = 10;
{
int a = 20;
printf("内部 a = %d\n", a);
}
printf("外部 a = %d\n", a);
六、分隔符 #
6.1 分号 #
每条语句以分号结尾:
c
int a = 10;
printf("Hello\n");
return 0;
6.2 大括号 #
用于定义语句块:
c
int main() {
if (condition) {
}
}
6.3 小括号 #
用于函数调用和表达式:
c
int result = add(1, 2);
int value = (a + b) * c;
6.4 中括号 #
用于数组:
c
int arr[10];
arr[0] = 1;
6.5 逗号 #
用于分隔:
c
int a = 1, b = 2, c = 3;
func(a, b, c);
七、空白字符 #
7.1 空白字符类型 #
- 空格
- 制表符
\t - 换行符
\n - 注释
7.2 空白字符的作用 #
分隔标识符:
c
int a = 10;
int a=10;
格式化代码:
c
int main() {
int a = 10;
int b = 20;
return 0;
}
忽略多余空白:
c
int a=10;
int a = 10 ;
int a = 10 ;
以上三种写法等价。
八、代码规范 #
8.1 缩进 #
使用4个空格或1个制表符:
c
int main() {
if (condition) {
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
}
return 0;
}
8.2 大括号风格 #
K&R风格(推荐):
c
if (condition) {
statement;
}
Allman风格:
c
if (condition)
{
statement;
}
8.3 行长度 #
每行不超过80-120个字符:
c
int result = very_long_function_name(param1, param2,
param3, param4);
8.4 空行使用 #
c
#include <stdio.h>
#define MAX_SIZE 100
int global_var;
int add(int a, int b) {
return a + b;
}
int main(void) {
int a = 10;
int b = 20;
int sum = add(a, b);
printf("sum = %d\n", sum);
return 0;
}
8.5 函数规范 #
c
/**
* @brief 计算两个整数的最大公约数
* @param a 第一个整数
* @param b 第二个整数
* @return 最大公约数
*/
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
九、预处理指令 #
9.1 #include #
包含头文件:
c
#include <stdio.h>
#include "myheader.h"
< >用于系统头文件" "用于用户头文件
9.2 #define #
定义宏:
c
#define PI 3.14159
#define MAX(a, b) ((a) > (b) ? (a) : (b))
9.3 条件编译 #
c
#ifdef DEBUG
printf("调试模式\n");
#endif
#ifndef HEADER_H
#define HEADER_H
#endif
#if VERSION >= 2
new_feature();
#endif
十、常见错误 #
10.1 中文符号 #
c
printf("Hello"); // 错误:使用了中文括号和分号
printf("Hello"); // 正确:使用英文符号
10.2 漏掉分号 #
c
int a = 10
int b = 20; // 错误:上一行缺少分号
10.3 大括号不匹配 #
c
int main() {
if (condition) {
printf("Hello\n");
// 错误:缺少右大括号
return 0;
}
10.4 大小写错误 #
c
Int a = 10; // 错误:Int应为int
10.5 关键字误用 #
c
int int = 10; // 错误:int是关键字,不能作为变量名
十一、总结 #
语法要点 #
| 要点 | 说明 |
|---|---|
| 注释 | //单行 /多行/ |
| 标识符 | 字母、数字、下划线,不能以数字开头 |
| 关键字 | 32个保留字,不能用作标识符 |
| 语句 | 以分号结尾 |
| 大小写 | 区分大小写 |
编码规范 #
- 使用有意义的命名
- 保持一致的缩进
- 添加必要的注释
- 每行不超过80-120字符
- 函数之间添加空行
学习建议 #
- 养成良好的编码习惯
- 使用代码格式化工具
- 多阅读优秀代码
- 注释要清晰准确
- 变量命名要有意义
下一步,让我们学习C语言的数据类型!
最后更新:2026-03-26