引言:C语言的魅力与挑战
C语言,作为一门历史悠久且应用广泛的编程语言,以其简洁、高效、灵活的特点,成为了计算机科学领域的基础。从操作系统到嵌入式系统,从游戏开发到高性能计算,C语言无处不在。然而,对于初学者来说,C语言的入门并不容易,需要从基础语法到复杂算法,一步步深入学习。本文将带你从入门到实战,通过50个经典实例,深度解析C语言编程。
第一章:C语言基础语法
1.1 数据类型与变量
#include <stdio.h>
int main() {
int age = 25;
float salary = 5000.0;
char grade = 'A';
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}
1.2 运算符与表达式
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
1.3 控制语句
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("Number is positive\n");
} else if (number < 0) {
printf("Number is negative\n");
} else {
printf("Number is zero\n");
}
return 0;
}
第二章:函数与模块化编程
2.1 函数定义与调用
#include <stdio.h>
void printMessage() {
printf("Hello, World!\n");
}
int main() {
printMessage();
return 0;
}
2.2 递归函数
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int number = 5;
printf("Factorial of %d is %d\n", number, factorial(number));
return 0;
}
第三章:指针与内存管理
3.1 指针基础
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void *)&a);
printf("Value of ptr: %p\n", (void *)ptr);
printf("Value of *ptr: %d\n", *ptr);
return 0;
}
3.2 动态内存分配
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("Value of ptr[%d]: %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
第四章:数组与字符串处理
4.1 一维数组
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("Value of arr[%d]: %d\n", i, arr[i]);
}
return 0;
}
4.2 字符串处理
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
printf("Concatenation: %s\n", strcat(str1, str2));
printf("Length: %lu\n", strlen(str1));
return 0;
}
第五章:结构体与联合体
5.1 结构体定义与使用
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p1;
strcpy(p1.name, "John");
p1.age = 30;
p1.salary = 5000.0;
printf("Name: %s\n", p1.name);
printf("Age: %d\n", p1.age);
printf("Salary: %.2f\n", p1.salary);
return 0;
}
5.2 联合体定义与使用
#include <stdio.h>
union Data {
int i;
float f;
char c[4];
};
int main() {
union Data u;
u.i = 10;
printf("Value of u.i: %d\n", u.i);
u.f = 3.14;
printf("Value of u.f: %.2f\n", u.f);
printf("Value of u.c: %s\n", u.c);
return 0;
}
第六章:文件操作
6.1 文件读取
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("File not found\n");
return 1;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
return 0;
}
6.2 文件写入
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("File not found\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
第七章:标准库函数
7.1 字符串函数
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
printf("Length of str1: %lu\n", strlen(str1));
printf("Concatenation: %s\n", strcat(str1, str2));
printf("Copy: %s\n", strcpy(str1, str2));
return 0;
}
7.2 数学函数
#include <stdio.h>
#include <math.h>
int main() {
printf("Square root of 16: %f\n", sqrt(16));
printf("Power of 2: %f\n", pow(2, 3));
return 0;
}
第八章:指针与数组
8.1 指针与数组的关系
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("Value of arr[%d]: %d\n", i, *(ptr + i));
}
return 0;
}
8.2 二维数组
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Value of arr[%d][%d]: %d\n", i, j, arr[i][j]);
}
}
return 0;
}
第九章:结构体与指针
9.1 结构体指针
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p1;
strcpy(p1.name, "John");
p1.age = 30;
p1.salary = 5000.0;
struct Person *ptr = &p1;
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Salary: %.2f\n", ptr->salary);
return 0;
}
9.2 结构体数组
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p1[2] = {
{"John", 30, 5000.0},
{"Jane", 25, 4000.0}
};
for (int i = 0; i < 2; i++) {
printf("Name: %s\n", p1[i].name);
printf("Age: %d\n", p1[i].age);
printf("Salary: %.2f\n", p1[i].salary);
}
return 0;
}
第十章:指针与函数
10.1 指针作为函数参数
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
10.2 函数指针
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("Sum: %d\n", ptr(10, 20));
return 0;
}
第十一章:动态内存分配
11.1 动态分配内存
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("Value of ptr[%d]: %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
11.2 内存释放
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
free(ptr);
return 0;
}
第十二章:文件操作
12.1 文件读取
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("File not found\n");
return 1;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
return 0;
}
12.2 文件写入
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("File not found\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
第十三章:标准库函数
13.1 字符串函数
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
printf("Length of str1: %lu\n", strlen(str1));
printf("Concatenation: %s\n", strcat(str1, str2));
printf("Copy: %s\n", strcpy(str1, str2));
return 0;
}
13.2 数学函数
#include <stdio.h>
#include <math.h>
int main() {
printf("Square root of 16: %f\n", sqrt(16));
printf("Power of 2: %f\n", pow(2, 3));
return 0;
}
第十四章:指针与数组
14.1 指针与数组的关系
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("Value of arr[%d]: %d\n", i, *(ptr + i));
}
return 0;
}
14.2 二维数组
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Value of arr[%d][%d]: %d\n", i, j, arr[i][j]);
}
}
return 0;
}
第十五章:结构体与指针
15.1 结构体指针
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p1;
strcpy(p1.name, "John");
p1.age = 30;
p1.salary = 5000.0;
struct Person *ptr = &p1;
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Salary: %.2f\n", ptr->salary);
return 0;
}
15.2 结构体数组
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p1[2] = {
{"John", 30, 5000.0},
{"Jane", 25, 4000.0}
};
for (int i = 0; i < 2; i++) {
printf("Name: %s\n", p1[i].name);
printf("Age: %d\n", p1[i].age);
printf("Salary: %.2f\n", p1[i].salary);
}
return 0;
}
第十六章:指针与函数
16.1 指针作为函数参数
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
16.2 函数指针
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("Sum: %d\n", ptr(10, 20));
return 0;
}
第十七章:动态内存分配
17.1 动态分配内存
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("Value of ptr[%d]: %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
17.2 内存释放
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
free(ptr);
return 0;
}
第十八章:文件操作
18.1 文件读取
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("File not found\n");
return 1;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
return 0;
}
18.2 文件写入
”`c
#include
int main() {
FILE *file = fopen("example.txt", "w");
if
