前言

这里只是简单列出C语言的知识点


1. 基础语法

1.1 数据类型

1
2
3
4
int a = 10;          // 整型
float b = 3.14; // 单精度浮点
double c = 3.141592; // 双精度浮点
char d = 'A'; // 字符型

1.2 运算符

  • 算术运算符:+ - * / %
  • 关系运算符:== != > < >= <=
  • 逻辑运算符:&& || !

2. 流程控制

2.1 条件语句

1
2
3
4
5
6
7
if(condition) {
// 代码块
} else if(condition2) {
// 代码块
} else {
// 代码块
}

2.2 循环结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// for循环
for(int i=0; i<10; i++) {
printf("%d\n", i);
}

// while循环
while(condition) {
// 代码块
}

// do-while循环
do {
// 代码块
} while(condition);

3. 函数

1
2
3
4
5
6
7
// 函数定义
int add(int a, int b) {
return a + b;
}

// 函数调用
int result = add(3, 5);

4. 数组与指针

4.1 数组

1
2
int arr[5] = {1,2,3,4,5}; // 一维数组
int matrix[2][3] = {{1,2,3}, {4,5,6}}; // 二维数组

4.2 指针

1
2
3
int var = 20;
int *ptr = &var; // 指针声明和赋值
printf("%d", *ptr); // 解引用

5. 结构体与联合体

5.1 结构体

1
2
3
4
5
6
7
struct Student {
char name[20];
int age;
float score;
};

struct Student stu1 = {"Alice", 20, 90.5};

5.2 联合体

1
2
3
4
5
union Data {
int i;
float f;
char str[20];
};

6. 文件操作

1
2
3
4
FILE *fptr;
fptr = fopen("file.txt", "w"); // 打开文件
fprintf(fptr, "Hello World"); // 写入文件
fclose(fptr); // 关闭文件

7. 内存管理

1
2
int *ptr = (int*)malloc(5*sizeof(int)); // 动态分配内存
free(ptr); // 释放内存

8. 预处理器

1
2
3
4
5
#define PI 3.14159   // 宏定义
#include <stdio.h> // 头文件包含
#ifdef DEBUG // 条件编译
// 调试代码
#endif

9. 常用标准库

  • <stdio.h>: 输入输出函数
  • <stdlib.h>: 内存分配等通用函数
  • <string.h>: 字符串处理函数
  • <math.h>: 数学函数

10. 重要特性

  • 面向过程编程
  • 直接内存访问能力
  • 高效的底层控制
  • 可移植性强