一、C语言基础
1.1 数据类型与变量
习题: 定义一个整型变量 age,并初始化为 25。
答案:
#include <stdio.h>
int main() {
int age = 25;
printf("My age is: %d\n", age);
return 0;
}
1.2 运算符
习题: 编写一个C程序,计算 3 + 5 * 2 的结果。
答案:
#include <stdio.h>
int main() {
int result = 3 + 5 * 2;
printf("The result is: %d\n", result);
return 0;
}
1.3 控制语句
习题: 使用 if 语句判断一个数是否为偶数。
答案:
#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;
}
二、函数
2.1 函数定义
习题: 编写一个函数,计算两个数的和。
答案:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int x = 10, y = 20, sum;
sum = add(x, y);
printf("The sum is: %d\n", sum);
return 0;
}
2.2 递归函数
习题: 编写一个递归函数,计算斐波那契数列的第 n 项。
答案:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d\n", fibonacci(n));
return 0;
}
三、指针
3.1 指针与数组
习题: 使用指针遍历一个数组并打印所有元素。
答案:
#include <stdio.h>
int main() {
int array[] = {1, 2, 3, 4, 5};
int *ptr = array;
int n = sizeof(array) / sizeof(array[0]);
for (int i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
printf("\n");
return 0;
}
3.2 函数指针
习题: 使用函数指针交换两个数的值。
答案:
#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;
}
四、结构体与联合体
4.1 结构体
习题: 定义一个学生结构体,并创建一个学生对象。
答案:
#include <stdio.h>
typedef struct {
char name[50];
int age;
float gpa;
} Student;
int main() {
Student student;
printf("Enter the name: ");
scanf("%s", student.name);
printf("Enter the age: ");
scanf("%d", &student.age);
printf("Enter the GPA: ");
scanf("%f", &student.gpa);
printf("Student: %s, Age: %d, GPA: %.2f\n", student.name, student.age, student.gpa);
return 0;
}
4.2 联合体
习题: 定义一个联合体,包含一个整数和一个浮点数,并演示如何访问它们。
答案:
#include <stdio.h>
typedef union {
int i;
float f;
} Data;
int main() {
Data data;
data.i = 10;
printf("Integer value: %d\n", data.i);
data.f = 10.5f;
printf("Float value: %.2f\n", data.f);
return 0;
}
五、文件操作
5.1 文件写入
习题: 创建一个文件并写入一些文本。
答案:
#include <stdio.h>
int main() {
FILE *fp;
char filename[] = "example.txt";
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error opening file!\n");
return -1;
}
fprintf(fp, "Hello, World!");
fclose(fp);
return 0;
}
5.2 文件读取
习题: 打开一个文件并读取其中的内容。
答案:
#include <stdio.h>
int main() {
FILE *fp;
char filename[] = "example.txt";
char content[100];
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file!\n");
return -1;
}
while (fgets(content, sizeof(content), fp)) {
printf("%s", content);
}
fclose(fp);
return 0;
}
六、动态内存分配
6.1 内存分配
习题: 使用 malloc 分配内存并创建一个数组。
答案:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int n = 5;
array = (int *)malloc(n * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed!\n");
return -1;
}
for (int i = 0; i < n; i++) {
array[i] = i + 1;
}
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
free(array);
return 0;
}
6.2 内存释放
习题: 释放已分配的内存。
答案:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int n = 5;
array = (int *)malloc(n * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed!\n");
return -1;
}
// ... 使用数组 ...
free(array);
return 0;
}
七、结构体与指针
7.1 结构体指针
习题: 使用结构体指针访问和修改结构体成员。
答案:
#include <stdio.h>
typedef struct {
char name[50];
int age;
} Person;
int main() {
Person person;
Person *ptr = &person;
printf("Enter the name: ");
scanf("%s", ptr->name);
printf("Enter the age: ");
scanf("%d", &ptr->age);
printf("Name: %s, Age: %d\n", ptr->name, ptr->age);
return 0;
}
7.2 指针数组
习题: 使用指针数组存储多个字符串。
答案:
#include <stdio.h>
int main() {
char *array[] = {"Hello", "World", "C", "Programming"};
int n = sizeof(array) / sizeof(array[0]);
for (int i = 0; i < n; i++) {
printf("%s\n", array[i]);
}
return 0;
}
八、C语言高级特性
8.1 预处理器
习题: 使用宏定义计算两个数的最大值。
答案:
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main() {
int x = 10, y = 20;
printf("The maximum value is: %d\n", MAX(x, y));
return 0;
}
8.2 位操作
习题: 使用位操作将一个整数的最低位设置为 1。
答案:
#include <stdio.h>
int main() {
int num = 5;
printf("Original number: %d\n", num);
num |= 1; // Set the lowest bit
printf("Number after setting the lowest bit: %d\n", num);
return 0;
}
九、总结
通过以上习题,相信你已经掌握了C语言编程的基本知识。在实际应用中,不断实践和总结是非常重要的。祝你编程顺利!
