int
存储整数,float
或double
存储浮点数。
int a = 10;
float b = 3.14;
if-else
,switch-case
)跟轮回语句(for
,while
,do-while
)来把持顺序流程。
if (a > b) {
printf("a is greater than b\n");
} else {
printf("a is not greater than b\n");
}
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
### 1.4 指针
- **技能**:控制指针的基本操纵,如申明、赋值、解引用跟指针运算。
- **代码示例**:
```c
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", *ptr);
char str[] = "Hello, World!";
printf("%s\n", str);
struct Person p = {“John Doe”, 30}; printf(“Name: %s, Age: %d\n”, p.name, p.age);
### 1.7 预处理器指令
- **技能**:利用预处理器指令(如`#define`,`#include`)来进步代码的可读性跟可保护性。
- **代码示例**:
```c
#define PI 3.14159
printf("Value of PI: %f\n", PI);
int *ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
char c;
while ((c = fgetc(file)) != EOF) {
putchar(c);
}
fclose(file);
printf
跟scanf
函数停止格局化输出跟数据读入。
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Sum: %d\n", x + y);
// main.c #include “module.h”
int main() {
int result = ADD(5, 3);
printf("Result: %d\n", result);
return 0;
}
### 1.12 编译与调试
- **技能**:懂得C言语的编译过程,并利用调试东西来排查顺序错误。
- **代码示例**:
```bash
gcc -o program program.c
./program
for (int i = 0; i < n; i += 4) {
// 优化后的轮回
}
struct Rectangle {
double width;
double height;
};
struct Shape *create_shape(int type) {
switch (type) {
case 1:
return (struct Shape *)malloc(sizeof(struct Circle));
case 2:
return (struct Shape *)malloc(sizeof(struct Rectangle));
default:
return NULL;
}
}
## 2. C言语罕见错曲剖析
### 2.1 缺乏分号
- **错误示例**:
```c
if (a > b)
printf("a is greater than b\n");
if
语句前面缺乏分号。if
语句前面增加分号。
int a = 10;
printf("Value of a: %d\n", a);
printf("Value of b: %d\n", b);
b
未申明。b
。int main() {
int result = add(5);
printf("Result: %d\n", result);
return 0;
}
- **原因**:函数`add`期望两个实参,但只转达了一个。
- **处理打算**:转达两个实参给`add`函数。
### 2.4 变量范例抵触
- **错误示例**:
```c
int a = 10;
float b = 3.14;
a = a + b;
float
范例的值赋给int
范例的变量,可能招致数据丧掉。a
转换为float
范例,然后再停止加法运算。
int *ptr;
printf("Value of ptr: %d\n", *ptr);
ptr
未初始化,拜访不决义的内存地点。malloc
或calloc
为指针分配内存。
int arr[5];
for (int i = 0; i < 10; i++) {
arr[i] = i;
}
arr
只有5个元素,但轮回中拜访了第10个元素。
int *ptr = (int *)malloc(sizeof(int));
*ptr = 10;
// 未能开释分配的内存
free
函数开释分配的内存。
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
char c;
while ((c = fgetc(file)) != EOF) {
putchar(c);
}
// 未能封闭文件
fclose
函数封闭文件。
int main() {
printf("Hello, World!\n");
return 0;
}
stdio.h
头文件。
gcc -o program program.c
-l
选项指定库文件。int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
- **原因**:函数`add`中的加法运算符写成了乘法运算符。
- **处理打算**:检查代码逻辑,确保正确利用运算符。
### 2.12 范例转换错误
- **错误示例**:
```c
int a = 10;
float b = 3.14;
printf("Sum: %d\n", a + b);
float
范例的值与int
范例的值相加,可能招致数据丧掉。int
范例转换为float
范例,然后再停止加法运算。
if (a > b > c)
printf("a is greater than b and c\n");
for (int i = 0; i <= 10; i++) {
printf("%d\n", i);
}
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
} “`
add
不前去值。