C语言作为一门历史悠久且应用广泛的编程语言,其精髓在于其简洁、高效和强大的功能。本文将深入剖析50个经典编程实例,并结合实战技巧,帮助读者全面掌握C语言的精髓。
实例一:C语言基础语法
- 变量声明与赋值
int a = 10;
float b = 3.14;
char c = 'A';
- 数据类型转换
int x = (int)3.14;
- 运算符
int a = 5, b = 3;
int sum = a + b; // 加法
int diff = a - b; // 减法
int prod = a * b; // 乘法
int div = a / b; // 除法
实例二:控制语句
- 条件语句(if-else)
if (a > b) {
printf("a 大于 b");
} else {
printf("a 小于 b");
}
- 循环语句(for、while、do-while)
// for 循环
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
// while 循环
int i = 0;
while (i < 10) {
printf("%d ", i);
i++;
}
// do-while 循环
int j = 0;
do {
printf("%d ", j);
j++;
} while (j < 10);
实例三:数组操作
- 一维数组
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
- 二维数组
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("%d ", arr[i][j]);
}
printf("\n");
}
实例四:指针与内存管理
- 指针变量
int a = 10;
int *ptr = &a;
printf("a 的地址:%p\n", (void *)ptr);
- 指针运算
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
- 内存分配与释放
int *p = (int *)malloc(sizeof(int));
*p = 10;
printf("分配的内存地址:%p,内存值为:%d\n", (void *)p, *p);
free(p);
实例五:函数与递归
- 函数定义与调用
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(3, 4);
printf("结果:%d\n", result);
return 0;
}
- 递归函数
int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int result = factorial(5);
printf("5 的阶乘:%d\n", result);
return 0;
}
实例六:结构体与联合体
- 结构体定义与使用
struct Student {
char name[50];
int age;
float score;
};
struct Student stu1;
strcpy(stu1.name, "张三");
stu1.age = 20;
stu1.score = 90.5;
printf("姓名:%s,年龄:%d,成绩:%f\n", stu1.name, stu1.age, stu1.score);
- 联合体定义与使用
union Data {
int i;
float f;
char c[4];
};
union Data u;
u.i = 10;
printf("整型:%d\n", u.i);
u.f = 3.14;
printf("浮点型:%f\n", u.f);
实例七:文件操作
- 文件打开与关闭
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("文件打开失败\n");
return 1;
}
fclose(fp);
- 文件读写
FILE *fp = fopen("example.txt", "w");
fprintf(fp, "Hello, World!");
fclose(fp);
fp = fopen("example.txt", "r");
char buffer[100];
fgets(buffer, 100, fp);
printf("%s\n", buffer);
fclose(fp);
实例八:字符串操作
- 字符串复制
char src[100] = "Hello, World!";
char dest[100];
strcpy(dest, src);
printf("dest: %s\n", dest);
- 字符串连接
char str1[100] = "Hello, ";
char str2[] = "World!";
strncat(str1, str2, strlen(str2));
printf("str1: %s\n", str1);
实例九:标准库函数
- 数学函数
#include <math.h>
double result = sqrt(16); // 计算平方根
printf("结果:%f\n", result);
- 字符串函数
#include <string.h>
char str1[100] = "Hello, World!";
char str2[] = "World!";
int index = strstr(str1, str2); // 查找子字符串
printf("子字符串位置:%d\n", index);
实例十:内存分配与释放
- malloc 与 free
int *p = (int *)malloc(sizeof(int));
*p = 10;
printf("分配的内存地址:%p,内存值为:%d\n", (void *)p, *p);
free(p);
- calloc 与 free
int *p = (int *)calloc(5, sizeof(int));
for (int i = 0; i < 5; i++) {
p[i] = i;
}
printf("分配的内存地址:%p,内存值为:%d\n", (void *)p, p[0]);
free(p);
实例十一:位操作
- 按位与
int a = 5; // 0101
int b = 3; // 0011
int result = a & b; // 0001
printf("结果:%d\n", result);
- 按位或
int a = 5; // 0101
int b = 3; // 0011
int result = a | b; // 0111
printf("结果:%d\n", result);
实例十二:结构体数组与指针
- 结构体数组
struct Student {
char name[50];
int age;
float score;
};
struct Student stu[3] = {
{"张三", 20, 90.5},
{"李四", 21, 85.5},
{"王五", 22, 95.5}
};
- 结构体指针
struct Student *ptr = stu;
for (int i = 0; i < 3; i++) {
printf("姓名:%s,年龄:%d,成绩:%f\n", ptr[i].name, ptr[i].age, ptr[i].score);
}
实例十三:动态内存分配与释放
- malloc 与 free
int *p = (int *)malloc(sizeof(int));
*p = 10;
printf("分配的内存地址:%p,内存值为:%d\n", (void *)p, *p);
free(p);
- calloc 与 free
int *p = (int *)calloc(5, sizeof(int));
for (int i = 0; i < 5; i++) {
p[i] = i;
}
printf("分配的内存地址:%p,内存值为:%d\n", (void *)p, p[0]);
free(p);
实例十四:函数指针
- 函数指针定义与使用
int add(int x, int y) {
return x + y;
}
int main() {
int (*func)(int, int) = add;
int result = func(3, 4);
printf("结果:%d\n", result);
return 0;
}
- 函数指针数组
int add(int x, int y) {
return x + y;
}
int sub(int x, int y) {
return x - y;
}
int main() {
int (*func[2])(int, int) = {add, sub};
printf("加法:%d\n", func[0](3, 4));
printf("减法:%d\n", func[1](3, 4));
return 0;
}
实例十五:结构体与函数指针
- 结构体指针与函数指针
struct Student {
char name[50];
int age;
float score;
};
void printStudent(struct Student *stu) {
printf("姓名:%s,年龄:%d,成绩:%f\n", stu->name, stu->age, stu->score);
}
int main() {
struct Student stu = {"张三", 20, 90.5};
printStudent(&stu);
return 0;
}
实例十六:函数指针与回调函数
- 回调函数
typedef void (*Callback)(int);
void callbackFunction(int x) {
printf("回调函数:%d\n", x);
}
void testCallback(Callback func, int x) {
func(x);
}
int main() {
testCallback(callbackFunction, 10);
return 0;
}
实例十七:多文件编程
- 头文件
// student.h
struct Student {
char name[50];
int age;
float score;
};
- 源文件
// student.c
#include "student.h"
void printStudent(struct Student *stu) {
printf("姓名:%s,年龄:%d,成绩:%f\n", stu->name, stu->age, stu->score);
}
- 主程序
// main.c
#include "student.h"
int main() {
struct Student stu = {"张三", 20, 90.5};
printStudent(&stu);
return 0;
}
实例十八:宏定义
- 宏定义
#define PI 3.1415926
#define MAX(a, b) ((a) > (b) ? (a) : (b))
- 宏展开
int a = 3, b = 4;
int max = MAX(a, b); // 展开为:int max = ((a) > (b) ? (a) : (b));
printf("最大值:%d\n", max);
实例十九:预处理指令
- 条件编译
#define DEBUG
#include "example.h"
- 宏定义
#define PI 3.1415926
- 文件包含
#include "example.h"
实例二十:预处理指令实战
- 宏定义
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
- 条件编译
#include "example.h"
#ifdef DEBUG
printf("调试模式\n");
#else
printf("非调试模式\n");
#endif
- 文件包含
#include "example.h"
实例二十一:结构体与枚举
- 结构体定义
struct Student {
char name[50];
int age;
float score;
};
- 枚举定义
enum Gender {
MALE,
FEMALE
};
- 结构体与枚举结合
struct Student {
char name[50];
int age;
float score;
enum Gender gender;
};
实例二十二:联合体与枚举
- 联合体定义
union Data {
int i;
float f;
char c[4];
};
- 枚举定义
enum Color {
RED,
GREEN,
BLUE
};
- 联合体与枚举结合
union Data {
int i;
float f;
char c[4];
enum Color color;
};
实例二十三:结构体数组与枚举
- 结构体定义
struct Student {
char name[50];
int age;
float score;
};
- 枚举定义
enum Gender {
MALE,
FEMALE
};
- 结构体数组与枚举结合
struct Student stu[3] = {
{"张三", 20, 90.5, MALE},
{"李四", 21, 85.5, FEMALE},
{"王五", 22, 95.5, MALE}
};
实例二十四:结构体指针与枚举
- 结构体定义
struct Student {
char name[50];
int age;
float score;
};
- 枚举定义
enum Gender {
MALE,
FEMALE
};
- 结构体指针与枚举结合
struct Student *ptr = stu;
ptr->gender = MALE;
实例二十五:结构体与位域
- 结构体定义
struct Student {
char name[50];
int age;
float score;
};
- 位域定义
struct BitField {
unsigned int id : 8;
unsigned int gender : 1;
unsigned int active : 1;
};
- 结构体与位域结合
struct Student {
char name[50];
int age;
float score;
struct BitField bitField;
};
实例二十六:结构体与链表
- 结构体定义
struct Student {
char name[50];
int age;
float score;
struct Student *next;
};
- 链表创建
struct Student *createStudentList() {
struct Student *head = NULL;
struct Student *temp = NULL;
for (int i = 0; i < 3; i++) {
temp = (struct Student *)malloc(sizeof(struct Student));
strcpy(temp->name, "张三");
temp->age = i + 1;
temp->score = 90.5 + i;
temp->next = head;
head = temp;
}
return head;
}
- 链表遍历
struct Student *head = createStudentList();
struct Student *temp = head;
while (temp != NULL) {
printf("姓名:%s,年龄:%d,成绩:%f\n", temp->name, temp->age, temp->score);
temp = temp->next;
}
实例二十七:结构体与树
- 结构体定义
struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
};
- 树创建
struct TreeNode *createTreeNode(int data) {
struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
- 树遍历
struct TreeNode *root = createTreeNode(1);
root->left = createTreeNode(2);
root->right = createTreeNode(3);
实例二十八:结构体与图
- 结构体定义
struct GraphNode {
int data;
struct GraphNode *next;
};
- 图创建
struct GraphNode *createGraphNode(int data) {
struct GraphNode *node = (struct GraphNode *)malloc(sizeof(struct GraphNode));
node->data = data;
node->next = NULL;
return node;
}
- 图遍历
struct GraphNode *root = createGraphNode(1);
root->next = createGraphNode(2);
root->next->next = createGraphNode(3);
实例二十九:结构体与散列表
- 结构体定义
struct HashNode {
int key;
int value;
struct HashNode *next;
};
- 散列表创建
struct HashNode *createHashNode(int key, int value) {
struct HashNode *node = (struct HashNode *)malloc(sizeof(struct HashNode));
node->key = key;
node->value = value;
node->next = NULL;
return node;
}
- 散列表遍历
struct HashNode *hashTable[10];
hashTable[0] = createHashNode(1, 10);
hashTable[1] = createHashNode(2, 20);
实例三十:结构体与排序算法
- 结构体定义
struct Student {
char name[50];
int age;
float score;
};
- 排序算法(冒泡排序)
”`c void bubbleSort(struct Student *stu
