引言
C语言作为一种历史悠久且应用广泛的编程语言,是学习计算机科学和软件开发的基础。本文将深入解析50个C语言编程的经典例题,帮助读者从入门到实战,逐步提升编程技能。
第1题:打印Hello World
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
这是C语言编程中最基础的例子,用于打印“Hello World!”到控制台。
第2题:变量赋值与输出
#include <stdio.h>
int main() {
int a = 10;
printf("The value of a is: %d\n", a);
return 0;
}
本例展示了变量的声明、赋值和输出。
第3题:数据类型转换
#include <stdio.h>
int main() {
int x = 5;
float y = 5.5;
printf("The sum is: %f\n", x + y);
return 0;
}
本例展示了不同数据类型之间的转换。
第4题:算术运算符
#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;
}
本例展示了C语言中的基本算术运算符。
第5题:条件语句
#include <stdio.h>
int main() {
int a = 10, b = 20;
if (a < b) {
printf("a is less than b\n");
} else {
printf("a is not less than b\n");
}
return 0;
}
本例展示了C语言中的条件语句。
第6题:循环结构
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
本例展示了C语言中的for循环。
第7题:数组操作
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
本例展示了数组的声明、初始化和操作。
第8题:指针操作
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Value of a through pointer: %d\n", *ptr);
return 0;
}
本例展示了指针的基本操作。
第9题:函数定义与调用
#include <stdio.h>
void printMessage() {
printf("Hello, world!\n");
}
int main() {
printMessage();
return 0;
}
本例展示了函数的定义和调用。
第10题:结构体操作
#include <stdio.h>
struct Student {
char name[50];
int age;
float score;
};
int main() {
struct Student s1;
strcpy(s1.name, "John Doe");
s1.age = 20;
s1.score = 85.5;
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Score: %.2f\n", s1.score);
return 0;
}
本例展示了结构体的定义、初始化和操作。
第11题:位运算
#include <stdio.h>
int main() {
int a = 5; // 0101
int b = 3; // 0011
printf("Bitwise AND: %d\n", a & b); // 0001
printf("Bitwise OR: %d\n", a | b); // 0111
printf("Bitwise XOR: %d\n", a ^ b); // 0110
printf("Bitwise NOT: %d\n", ~a); // 1110
return 0;
}
本例展示了C语言中的位运算。
第12题:文件操作
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
fprintf(fp, "Hello, world!\n");
fclose(fp);
return 0;
}
本例展示了C语言中的文件操作。
第13题:动态内存分配
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int));
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("%d\n", ptr[i]);
}
free(ptr);
return 0;
}
本例展示了C语言中的动态内存分配。
第14题:字符串操作
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char *str3 = strcat(str1, str2);
printf("Concatenated string: %s\n", str3);
return 0;
}
本例展示了C语言中的字符串操作。
第15题:结构体数组
#include <stdio.h>
struct Student {
char name[50];
int age;
float score;
};
int main() {
struct Student students[2] = {
{"John Doe", 20, 85.5},
{"Jane Smith", 22, 90.0}
};
for (int i = 0; i < 2; i++) {
printf("Name: %s, Age: %d, Score: %.2f\n", students[i].name, students[i].age, students[i].score);
}
return 0;
}
本例展示了结构体数组的操作。
第16题:函数指针
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*funcPtr)(int, int) = add;
printf("Sum: %d\n", funcPtr(5, 3));
return 0;
}
本例展示了函数指针的使用。
第17题:递归函数
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
本例展示了递归函数的使用。
第18题:指针数组
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *ptrs[2] = {&a, &b};
printf("Value of a: %d\n", *ptrs[0]);
printf("Value of b: %d\n", *ptrs[1]);
return 0;
}
本例展示了指针数组的操作。
第19题:函数重载
#include <stdio.h>
void printValue(int value) {
printf("Integer value: %d\n", value);
}
void printValue(float value) {
printf("Float value: %.2f\n", value);
}
int main() {
printValue(5);
printValue(3.14);
return 0;
}
本例展示了函数重载的使用。
第20题:枚举类型
#include <stdio.h>
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main() {
enum Weekday today = Wednesday;
printf("Today is: %d\n", today);
return 0;
}
本例展示了枚举类型的使用。
第21题:联合体
#include <stdio.h>
union Data {
int i;
float f;
char c[4];
};
int main() {
union Data data;
data.i = 10;
printf("Integer value: %d\n", data.i);
data.f = 3.14;
printf("Float value: %.2f\n", data.f);
return 0;
}
本例展示了联合体的使用。
第22题:结构体嵌套
#include <stdio.h>
struct Address {
char street[50];
char city[20];
char state[20];
int pincode;
};
struct Student {
char name[50];
int age;
struct Address addr;
};
int main() {
struct Student s1;
strcpy(s1.name, "John Doe");
s1.age = 20;
strcpy(s1.addr.street, "123 Main St");
strcpy(s1.addr.city, "Anytown");
strcpy(s1.addr.state, "CA");
s1.addr.pincode = 12345;
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Street: %s\n", s1.addr.street);
printf("City: %s\n", s1.addr.city);
printf("State: %s\n", s1.addr.state);
printf("Pincode: %d\n", s1.addr.pincode);
return 0;
}
本例展示了结构体嵌套的使用。
第23题:指针与结构体
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student s1;
struct Student *ptr = &s1;
strcpy(s1.name, "John Doe");
s1.age = 20;
printf("Name: %s\n", (*ptr).name);
printf("Age: %d\n", (*ptr).age);
return 0;
}
本例展示了指针与结构体的使用。
第24题:字符串处理函数
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
printf("Length of str1: %lu\n", strlen(str1));
printf("Concatenated string: %s\n", strcat(str1, str2));
printf("Copied string: %s\n", strcpy(str1, str2));
printf("Strcmp result: %d\n", strcmp(str1, str2));
return 0;
}
本例展示了C语言中的字符串处理函数。
第25题:内存分配与释放
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int));
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("%d\n", ptr[i]);
}
free(ptr);
return 0;
}
本例展示了C语言中的内存分配与释放。
第26题:文件读写
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
fprintf(fp, "Hello, world!\n");
fclose(fp);
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
char buffer[100];
fgets(buffer, sizeof(buffer), fp);
printf("Read from file: %s\n", buffer);
fclose(fp);
return 0;
}
本例展示了C语言中的文件读写操作。
第27题:链表操作
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertAtBeginning(struct Node **head, int data) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(struct Node *head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
struct Node *head = NULL;
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
printList(head);
return 0;
}
本例展示了C语言中的链表操作。
第28题:树结构操作
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
struct Node *createNode(int data) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void insert(struct Node **root, int data) {
if (*root == NULL) {
*root = createNode(data);
} else if (data < (*root)->data) {
insert(&((*root)->left), data);
} else {
insert(&((*root)->right), data);
}
}
void inorderTraversal(struct Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node *root = NULL;
insert(&root, 5);
insert(&root, 3);
insert(&root, 7);
insert(&root, 2);
insert(&root, 4);
insert(&root, 6);
insert(&root, 8);
inorderTraversal(root);
return 0;
}
本例展示了C语言中的树结构操作。
第29题:排序算法
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
本例展示了C语言中的冒泡排序算法。
第30题:查找算法
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) {
return m;
} else if (arr[m] < x) {
l = m + 1;
} else {
r = m - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) {
printf("Element is not present in array\n");
} else {
printf("Element is present at index %d\n", result);
}
return 0;
}
本例展示了C语言中的二分查找算法。
第31题:递归算法
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
本例展示了C语言中的递归算法。
第32题:动态规划
”`c
#include
int lcs(char *X, char *Y, int m, int n) {
int L[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
L[i][j] = 0;
} else if (X[i - 1] == Y[j - 1]) {
L[i][j] = L[i - 1][j - 1] + 1;
} else {
L[i][j] = (L[i - 1][j] > L[i][j - 1]) ? L[i - 1
