案例一:简单的“Hello, World!”程序
案例描述
编写一个C语言程序,输出“Hello, World!”。
实现代码
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
案例解析
这个案例是最基础的C语言程序,通过使用printf函数输出文本。
案例二:变量与数据类型
案例描述
编写一个C语言程序,定义并初始化几个变量,并输出它们的值。
实现代码
#include <stdio.h>
int main() {
int age = 25;
float height = 1.75f;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
案例解析
在这个案例中,我们学习了如何定义不同数据类型的变量,并使用printf函数输出它们的值。
案例三:运算符与表达式
案例描述
编写一个C语言程序,计算两个数的和、差、积、商。
实现代码
#include <stdio.h>
int main() {
int a = 10;
int b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用运算符进行基本的数学运算。
案例四:条件语句
案例描述
编写一个C语言程序,根据用户输入的年龄判断是否成年。
实现代码
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
案例解析
在这个案例中,我们学习了如何使用if语句进行条件判断。
案例五:循环语句
案例描述
编写一个C语言程序,使用循环语句输出1到10的数字。
实现代码
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
案例解析
在这个案例中,我们学习了如何使用for循环语句来重复执行一段代码。
案例六:函数
案例描述
编写一个C语言程序,定义一个函数计算两个数的和,并在主函数中调用它。
实现代码
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int a = 10;
int b = 5;
int result = sum(a, b);
printf("The sum is: %d\n", result);
return 0;
}
案例解析
在这个案例中,我们学习了如何定义和使用函数。
案例七:指针
案例描述
编写一个C语言程序,使用指针交换两个变量的值。
实现代码
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10;
int 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;
}
案例解析
在这个案例中,我们学习了如何使用指针来操作内存地址。
案例八:结构体
案例描述
编写一个C语言程序,定义一个结构体来存储学生信息,并创建一个学生对象。
实现代码
#include <stdio.h>
typedef struct {
char name[50];
int age;
float score;
} Student;
int main() {
Student student1;
strcpy(student1.name, "John");
student1.age = 20;
student1.score = 85.5;
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Score: %.2f\n", student1.score);
return 0;
}
案例解析
在这个案例中,我们学习了如何定义和使用结构体。
案例九:数组
案例描述
编写一个C语言程序,使用数组存储和输出10个整数。
实现代码
#include <stdio.h>
int main() {
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i;
for (i = 0; i < 10; i++) {
printf("%d\n", numbers[i]);
}
return 0;
}
案例解析
在这个案例中,我们学习了如何定义和使用数组。
案例十:字符串
案例描述
编写一个C语言程序,使用字符串函数输出用户输入的名字。
实现代码
#include <stdio.h>
#include <string.h>
int main() {
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0; // Remove newline character
printf("Your name is: %s\n", name);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用字符串函数来处理用户输入。
案例十一:文件操作
案例描述
编写一个C语言程序,创建一个文本文件并写入一些内容。
实现代码
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
fprintf(file, "This is a test file.\n");
fclose(file);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用文件操作函数来创建和写入文件。
案例十二:动态内存分配
案例描述
编写一个C语言程序,使用动态内存分配创建一个字符串数组,并输出其内容。
实现代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int size = 5;
char **names = (char **)malloc(size * sizeof(char *));
if (names == NULL) {
printf("Error allocating memory.\n");
return 1;
}
names[0] = "John";
names[1] = "Jane";
names[2] = "Alice";
names[3] = "Bob";
names[4] = "Charlie";
for (int i = 0; i < size; i++) {
printf("%s\n", names[i]);
}
free(names);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用malloc和free函数进行动态内存分配。
案例十三:结构体数组
案例描述
编写一个C语言程序,使用结构体数组存储和输出多个学生信息。
实现代码
#include <stdio.h>
typedef struct {
char name[50];
int age;
float score;
} Student;
int main() {
Student students[3] = {
{"John", 20, 85.5},
{"Jane", 22, 90.0},
{"Alice", 19, 78.0}
};
for (int i = 0; i < 3; i++) {
printf("Name: %s, Age: %d, Score: %.2f\n", students[i].name, students[i].age, students[i].score);
}
return 0;
}
案例解析
在这个案例中,我们学习了如何使用结构体数组来存储和操作多个结构体对象。
案例十四:指针数组
案例描述
编写一个C语言程序,使用指针数组存储和输出多个字符串。
实现代码
#include <stdio.h>
#include <string.h>
int main() {
char *names[] = {"John", "Jane", "Alice", "Bob", "Charlie"};
int size = sizeof(names) / sizeof(names[0]);
for (int i = 0; i < size; i++) {
printf("%s\n", names[i]);
}
return 0;
}
案例解析
在这个案例中,我们学习了如何使用指针数组来存储和操作多个字符串。
案例十五:链表
案例描述
编写一个C语言程序,使用链表存储和输出一系列整数。
实现代码
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert(Node **head, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
void freeList(Node *head) {
Node *current = head;
while (current != NULL) {
Node *next = current->next;
free(current);
current = next;
}
}
int main() {
Node *head = NULL;
insert(&head, 5);
insert(&head, 10);
insert(&head, 15);
printList(head);
freeList(head);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用链表来存储和操作一系列整数。
案例十六:递归
案例描述
编写一个C语言程序,使用递归函数计算阶乘。
实现代码
#include <stdio.h>
int factorial(int n) {
if (n <= 1) {
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;
}
案例解析
在这个案例中,我们学习了如何使用递归函数来计算阶乘。
案例十七:排序算法
案例描述
编写一个C语言程序,使用冒泡排序算法对一组整数进行排序。
实现代码
#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;
}
案例解析
在这个案例中,我们学习了如何使用冒泡排序算法对一组整数进行排序。
案例十八:查找算法
案例描述
编写一个C语言程序,使用二分查找算法在一个有序数组中查找一个特定的元素。
实现代码
#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语言程序,使用字符串处理函数将用户输入的字符串转换为大写。
实现代码
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; i < strlen(str); i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s\n", str);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用字符串处理函数toupper将字符串转换为大写。
案例二十:日期和时间
案例描述
编写一个C语言程序,使用time.h库获取当前时间和日期。
实现代码
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("Current date and time: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用time.h库获取当前时间和日期。
案例二十一:多线程
案例描述
编写一个C语言程序,使用pthread库创建两个线程,分别输出不同的消息。
实现代码
#include <stdio.h>
#include <pthread.h>
void *threadFunction(void *arg) {
printf("Thread %ld: %s\n", pthread_self(), (char *)arg);
return NULL;
}
int main() {
pthread_t thread1, thread2;
char *message1 = "Hello from thread 1!";
char *message2 = "Hello from thread 2!";
pthread_create(&thread1, NULL, threadFunction, (void *)message1);
pthread_create(&thread2, NULL, threadFunction, (void *)message2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
案例解析
在这个案例中,我们学习了如何使用pthread库创建和管理多线程。
案例二十二:信号处理
案例描述
编写一个C语言程序,使用signal函数处理SIGINT信号。
实现代码
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handleSignal(int sig) {
printf("Signal %d received.\n", sig);
_exit(0);
}
int main() {
signal(SIGINT, handleSignal);
while (1) {
printf("Press Ctrl+C to exit.\n");
sleep(1);
}
return 0;
}
案例解析
在这个案例中,我们学习了如何使用signal函数处理信号。
案例二十三:文件共享
案例描述
编写一个C语言程序,使用flock函数实现文件锁。
实现代码
”`c
#include
int main() {
int fd = open("example.txt", O_RDWR | O_CREAT, 0644);
if (fd == -1) {
perror("Error opening file");
return 1;
}
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("Error setting lock");
close(fd);
return 1;
}
printf("Lock acquired.\n");
// Perform operations on the file...
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("Error releasing lock");
close(fd);
