C语言是一种通用的、过程式的计算机编程语言,支持结构化编程、词法变量作用域和递归,允许函数自由地处理内存中的数据,特别是在数组方面。
每个C程序都包含至少一个函数,即main()
函数,这是程序的入口点。
c#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>
是预处理器指令,用于包含标准输入输出库的头文件。int main()
定义了主函数,是程序开始执行的地方。printf()
函数用于打印信息到控制台。C语言提供了多种基本的数据类型:
int
, short
, long
, long long
float
, double
char
_Bool
或使用stdbool.h
中的bool
定义cint age = 30;
float height = 5.7;
char initial = 'J';
_Bool isStudent = 1; // 或者 #include <stdbool.h> 并使用 bool, true, false
必须先声明变量才能使用它们。
cint a, b, c; // 可以在同一行声明多个相同类型的变量
a = 10;
b = 20;
C语言支持各种运算符,包括算术运算符(+
, -
, *
, /
, %
)、关系运算符(==
, !=
, >
, <
, >=
, <=
)、逻辑运算符(&&
, ||
, !
)等。
cif (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
for
循环cfor (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
while
循环cint j = 0;
while (j < 5) {
printf("%d\n", j);
j++;
}
do...while
循环cint k = 0;
do {
printf("%d\n", k);
k++;
} while (k < 5);
数组是一系列相同类型的元素的集合。
cint numbers[5] = {1, 2, 3, 4, 5};
printf("%d\n", numbers[0]); // 输出: 1
函数是完成特定任务的代码块。
cint add(int a, int b) {
return a + b;
}
int result = add(5, 3);
printf("Result: %d\n", result); // 输出: Result: 8
指针存储另一个变量的地址。
cint var = 20;
int *ptr = &var; // ptr 现在持有 var 的地址
printf("Value of var: %d\n", *ptr); // 使用 * 来访问指针指向的值
结构体允许你将不同类型的数据组合在一起。
cstruct Person {
char name[50];
int age;
float height;
};
struct Person person1 = {"John Doe", 30, 5.7};
printf("Name: %s\n", person1.name);
通过标准库中的fopen
, fclose
, fprintf
, fscanf
等函数可以对文件进行读写操作。
cFILE *file;
file = fopen("example.txt", "w"); // 打开文件用于写入
if (file == NULL) {
printf("无法打开文件\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
如#include
和#define
。
c#define PI 3.14159
在C语言中,可以通过标准库函数malloc
, calloc
, realloc
和free
来手动管理内存。
malloc
分配内存:cint *arr;
int n = 5;
arr = (int*) malloc(n * sizeof(int)); // 分配内存给n个整数
if(arr == NULL) {
printf("内存分配失败\n");
return 1;
}
// 使用完后释放内存
free(arr);
calloc
初始化内存:carr = (int*) calloc(n, sizeof(int)); // 分配并初始化为0
realloc
调整已分配内存大小:carr = (int*) realloc(arr, 10 * sizeof(int)); // 调整大小以容纳10个整数
在C语言中,数组名本质上是指向数组第一个元素的指针。
cint arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // 等价于 &arr[0]
printf("%d\n", *(ptr + 2)); // 输出: 3
虽然C没有内置的字符串类型,但可以使用字符数组来表示字符串。C标准库提供了丰富的字符串处理函数(如strcpy
, strcat
, strlen
等)。
cchar src[] = "Hello";
char dest[6];
strcpy(dest, src);
printf("%s\n", dest); // 输出: Hello
cchar str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s\n", str1); // 输出: Hello, World!
结构体允许你将不同类型的数据组合在一起。
cstruct Date {
int day;
int month;
int year;
};
struct Date today = {9, 4, 2025}; // 初始化结构体变量
printf("Today's date: %d/%d/%d\n", today.day, today.month, today.year);
联合体是一种特殊的数据格式,它可以在同一块内存中存储不同类型的数据,但任意时刻只能存储其中一种类型的数据。
cunion Data {
int i;
float f;
char str[20];
};
union Data data;
data.i = 10;
printf("%d\n", data.i); // 输出: 10
data.f = 220.5;
printf("%f\n", data.f); // 输出: 220.500000
枚举用于定义一组命名的整数常量。
cenum Weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum Weekday today = Monday;
printf("Day number: %d\n", today); // 输出: Day number: 1
除了前面提到的基本文件操作外,还可以进行更复杂的文件读写操作,比如读取整个文件或按行读取。
cFILE *file;
char line[256];
file = fopen("example.txt", "r");
if (file == NULL) {
printf("无法打开文件\n");
return 1;
}
while(fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
C语言本身不提供异常处理机制,但可以通过返回值和全局变量errno
来进行错误处理。
c#include <errno.h>
#include <stdio.h>
FILE *file = fopen("nonexistent.txt", "r");
if (file == NULL) {
printf("Error opening file: %s\n", strerror(errno));
return 1;
}
fclose(file);
main
函数可以接受命令行参数。
cint main(int argc, char *argv[]) {
for(int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
当你运行程序时,例如./program arg1 arg2
,argc
将是3,argv
将包含"./program"
, "arg1"
, 和 "arg2"
。
除了#include
和#define
,还有其他有用的预处理器指令。
c#define DEBUG
#ifdef DEBUG
printf("Debugging mode is on\n");
#endif
c#ifndef MYHEADER_H
#define MYHEADER_H
// Header file content
#endif