数据类型与变量
1.1 数据类型概述
在C语言中,数据类型定义了变量的存储大小和操作方式。C语言的数据类型主要包括:
- 整型(int)
- 字符型(char)
- 浮点型(float, double)
- 枚举型(enum)
- 指针型(void*)
1.2 变量的声明与初始化
int age = 18;
char grade = 'A';
float score = 88.5;
enum weekday { MON, TUE, WED, THU, FRI, SAT, SUN };
1.3 习题:计算一个整数a和b的最大公约数
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int a = 24, b = 18;
printf("The GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}
控制语句
2.1 条件语句
if (条件) {
// 条件为真时执行的代码
} else {
// 条件为假时执行的代码
}
2.2 循环语句
2.2.1 for循环
for (初始化; 条件; 迭代) {
// 循环体
}
2.2.2 while循环
while (条件) {
// 循环体
}
2.2.3 do-while循环
do {
// 循环体
} while (条件);
2.3 习题:打印1到10的整数
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
函数
3.1 函数的定义与调用
// 函数定义
int add(int a, int b) {
return a + b;
}
// 函数调用
int result = add(5, 6);
printf("The result is %d\n", result);
3.2 习题:编写一个函数,计算两个整数的和
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2);
printf("The sum is %d\n", sum);
return 0;
}
数组
4.1 一维数组
int arr[5] = {1, 2, 3, 4, 5};
4.2 二维数组
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
4.3 习题:计算二维数组中所有元素的和
#include <stdio.h>
int sum_array(int rows, int cols, int arr[rows][cols]) {
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}
return sum;
}
int main() {
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int sum = sum_array(3, 4, arr);
printf("The sum of all elements in the array is %d\n", sum);
return 0;
}
字符串
5.1 字符串的定义与初始化
char str1[] = "Hello";
char str2[10] = "World";
5.2 字符串的输入与输出
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("You entered: %s\n", str);
return 0;
}
5.3 习题:比较两个字符串是否相等
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
指针
6.1 指针的概念
指针是一个变量,用来存储另一个变量的地址。
6.2 指针的声明与赋值
int a = 10;
int *ptr = &a;
6.3 习题:通过指针访问数组元素
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
int *ptr = arr;
for (int i = 0; i < 3; i++) {
printf("%d ", *(ptr + i));
}
printf("\n");
return 0;
}
结构体
7.1 结构体的定义与使用
struct Student {
char name[50];
int age;
float score;
};
7.2 习题:创建一个结构体数组,并打印所有元素
#include <stdio.h>
struct Student {
char name[50];
int age;
float score;
};
int main() {
struct Student students[2] = {
{"Alice", 18, 85.5},
{"Bob", 19, 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;
}
动态内存分配
8.1 动态内存分配函数
void* malloc(size_t size);
void* calloc(size_t nmemb, size_t size);
void free(void* ptr);
8.2 习题:动态分配一个整数数组,并初始化所有元素为0
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int*)malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = 0;
}
free(arr);
return 0;
}
文件操作
9.1 文件打开
FILE *fptr = fopen("filename.txt", "r");
9.2 文件读写
fscanf(fptr, "%d", &num);
fprintf(fptr, "%d", num);
9.3 文件关闭
fclose(fptr);
9.4 习题:读取文件内容并打印
#include <stdio.h>
int main() {
FILE *fptr = fopen("filename.txt", "r");
if (fptr == NULL) {
printf("File opening failed!\n");
return 1;
}
char ch;
while ((ch = fgetc(fptr)) != EOF) {
putchar(ch);
}
fclose(fptr);
return 0;
}
实战案例
10.1 计算器程序
#include <stdio.h>
#include <stdlib.h>
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
}
double multiply(double a, double b) {
return a * b;
}
double divide(double a, double b) {
if (b == 0) {
printf("Error: Division by zero!\n");
exit(1);
}
return a / b;
}
int main() {
char op;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
switch (op) {
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
result = divide(num1, num2);
break;
default:
printf("Error: Invalid operator!\n");
exit(1);
}
printf("Result: %lf\n", result);
return 0;
}
10.2 字符串排序程序
#include <stdio.h>
#include <string.h>
void sort_strings(char str[][100], int n) {
char temp[100];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
}
int main() {
char strings[5][100] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
int n = sizeof(strings) / sizeof(strings[0]);
sort_strings(strings, n);
printf("Sorted strings:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
以上是C语言入门阶段的一些常见基础习题详解及实战案例。希望这些内容能帮助你更好地学习和理解C语言。