引言
C语言作为一门历史悠久且应用广泛的编程语言,一直是计算机科学领域的基础。对于初学者来说,通过实战案例学习C语言是一种非常有效的方法。本文将为你详细介绍50个实战案例,帮助你从小白成长为编程高手。
实战案例详解
案例一:计算两个数的和
目标:掌握基本的变量声明和运算符使用。
#include <stdio.h>
int main() {
int a = 10, b = 20, sum;
sum = a + b;
printf("The sum of %d and %d is %d.\n", a, b, sum);
return 0;
}
案例二:判断一个数是否为偶数
目标:理解逻辑运算符和条件语句。
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}
return 0;
}
案例三:冒泡排序
目标:掌握数组的操作和排序算法。
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
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;
}
案例四:递归计算阶乘
目标:理解递归函数的概念。
#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;
}
案例五:使用指针交换两个数的值
目标:理解指针的概念和操作。
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int num1 = 10, num2 = 20;
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swap: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
案例六:结构体和指针
目标:理解结构体和指针的使用。
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
void printStudent(struct Student *s) {
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
printf("Marks: %.2f\n", s->marks);
}
int main() {
struct Student s1 = {"John", 20, 90.5};
printStudent(&s1);
return 0;
}
案例七:文件操作
目标:掌握文件的读写操作。
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
案例八:动态内存分配
目标:理解动态内存分配的概念。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Error! unable to allocate memory\n");
exit(0);
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
free(ptr);
return 0;
}
案例九:链表操作
目标:理解链表的概念和操作。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 4);
printf("Created Linked list is: ");
printList(head);
return 0;
}
案例十:函数指针
目标:理解函数指针的概念。
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("Result: %d\n", ptr(3, 4));
return 0;
}
案例十一:结构体指针
目标:理解结构体指针的概念。
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
void printStudent(struct Student *s) {
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
printf("Marks: %.2f\n", s->marks);
}
int main() {
struct Student s1 = {"John", 20, 90.5};
printStudent(&s1);
return 0;
}
案例十二:文件操作
目标:掌握文件的读写操作。
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
案例十三:动态内存分配
目标:理解动态内存分配的概念。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Error! unable to allocate memory\n");
exit(0);
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
free(ptr);
return 0;
}
案例十四:链表操作
目标:理解链表的概念和操作。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 4);
printf("Created Linked list is: ");
printList(head);
return 0;
}
案例十五:函数指针
目标:理解函数指针的概念。
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("Result: %d\n", ptr(3, 4));
return 0;
}
案例十六:结构体指针
目标:理解结构体指针的概念。
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
void printStudent(struct Student *s) {
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
printf("Marks: %.2f\n", s->marks);
}
int main() {
struct Student s1 = {"John", 20, 90.5};
printStudent(&s1);
return 0;
}
案例十七:文件操作
目标:掌握文件的读写操作。
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
案例十八:动态内存分配
目标:理解动态内存分配的概念。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Error! unable to allocate memory\n");
exit(0);
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
free(ptr);
return 0;
}
案例十九:链表操作
目标:理解链表的概念和操作。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 4);
printf("Created Linked list is: ");
printList(head);
return 0;
}
案例二十:函数指针
目标:理解函数指针的概念。
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("Result: %d\n", ptr(3, 4));
return 0;
}
案例二十一:结构体指针
目标:理解结构体指针的概念。
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
void printStudent(struct Student *s) {
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
printf("Marks: %.2f\n", s->marks);
}
int main() {
struct Student s1 = {"John", 20, 90.5};
printStudent(&s1);
return 0;
}
案例二十二:文件操作
目标:掌握文件的读写操作。
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
案例二十三:动态内存分配
目标:理解动态内存分配的概念。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Error! unable to allocate memory\n");
exit(0);
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
free(ptr);
return 0;
}
案例二十四:链表操作
目标:理解链表的概念和操作。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 4);
printf("Created Linked list is: ");
printList(head);
return 0;
}
案例二十五:函数指针
目标:理解函数指针的概念。
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("Result: %d\n", ptr(3, 4));
return 0;
}
案例二十六:结构体指针
目标:理解结构体指针的概念。
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
void printStudent(struct Student *s) {
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
printf("Marks: %.2f\n", s->marks);
}
int main() {
struct Student s1 = {"John", 20, 90.5};
printStudent(&s1);
return 0;
}
案例二十七:文件操作
目标:掌握文件的读写操作。
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
案例二十八:动态内存分配
目标:理解动态内存分配的概念。
”`c
#include
int main() {
int *ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Error! unable to allocate memory\n");
exit(0);
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("You entered:\n");
for (i = 0; i < n; i++) {
