1. 打印“Hello, World!”
C语言编程的第一步通常是打印出“Hello, World!”。这个案例可以帮助你熟悉C语言的打印语句。
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
2. 计算两个数的和
这个案例将教你如何使用变量和运算符来计算两个数的和。
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Sum = %d\n", sum);
return 0;
}
3. 用户信息录入
在这个案例中,我们将学习如何从用户那里获取信息,并存储在变量中。
#include <stdio.h>
int main() {
char name[50];
int age;
float salary;
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your salary: ");
scanf("%f", &salary);
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
return 0;
}
4. 简单的加法器
创建一个简单的加法器,用户可以输入任意数量的数字,程序将计算并显示总和。
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter numbers (enter -1 to stop): ");
while (1) {
scanf("%d", &num);
if (num == -1) {
break;
}
sum += num;
}
printf("Sum = %d\n", sum);
return 0;
}
5. 温度转换器
编写一个程序,将华氏温度转换为摄氏温度。
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
6. 阶乘计算
计算一个给定整数的阶乘。
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
7. 检查素数
编写一个程序,检查一个给定的整数是否是素数。
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
8. 简单的猜数字游戏
创建一个简单的猜数字游戏,用户尝试猜测一个随机生成的数字。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, number_of_guesses = 0;
srand(time(0));
number = rand() % 100 + 1;
printf("Guess the number between 1 and 100: ");
scanf("%d", &guess);
while (guess != number) {
number_of_guesses++;
if (guess < number)
printf("Too low, try again: ");
else
printf("Too high, try again: ");
scanf("%d", &guess);
}
printf("Congratulations! You guessed the number in %d attempts.\n", number_of_guesses);
return 0;
}
9. 字符串长度计算
编写一个函数来计算字符串的长度。
#include <stdio.h>
#include <string.h>
int stringLength(const char *str) {
return strlen(str);
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%99s", str);
printf("Length of the string: %d\n", stringLength(str));
return 0;
}
10. 字符串比较
比较两个字符串是否相等。
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
scanf("%99s", str1);
printf("Enter second string: ");
scanf("%99s", str2);
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
11. 字符串复制
复制一个字符串到另一个字符串。
#include <stdio.h>
#include <string.h>
int main() {
char source[100], destination[100];
printf("Enter a string: ");
scanf("%99s", source);
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
12. 字符串连接
连接两个字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], result[200];
printf("Enter first string: ");
scanf("%99s", str1);
printf("Enter second string: ");
scanf("%99s", str2);
strcat(result, str1);
strcat(result, str2);
printf("Concatenated string: %s\n", result);
return 0;
}
13. 字符串查找
在字符串中查找一个子字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str[100], substr[100];
printf("Enter a string: ");
scanf("%99s", str);
printf("Enter substring to find: ");
scanf("%99s", substr);
if (strstr(str, substr) != NULL)
printf("Substring found.\n");
else
printf("Substring not found.\n");
return 0;
}
14. 数组操作
对数组进行排序和搜索。
#include <stdio.h>
void sortArray(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[5];
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++)
scanf("%d", &arr[i]);
sortArray(arr, 5);
printf("Sorted array: ");
for (int i = 0; i < 5; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
15. 二维数组操作
使用二维数组来存储和打印一个矩阵。
#include <stdio.h>
int main() {
int rows, cols, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
16. 结构体使用
使用结构体来存储和操作复杂数据。
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp;
printf("Enter employee name: ");
scanf("%49s", emp.name);
printf("Enter employee age: ");
scanf("%d", &emp.age);
printf("Enter employee salary: ");
scanf("%f", &emp.salary);
printf("Employee details:\n");
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
17. 链表操作
创建一个简单的链表,并添加和删除节点。
#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 deleteNode(struct Node** head_ref, int key) {
struct Node *temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
printf("Created linked list: ");
printList(head);
deleteNode(&head, 2);
printf("Linked list after deleting 2: ");
printList(head);
return 0;
}
18. 指针基础
学习如何使用指针操作变量。
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void*)&a);
printf("Value of ptr: %p\n", (void*)ptr);
printf("Value pointed by ptr: %d\n", *ptr);
*ptr = 20;
printf("New value of a: %d\n", a);
return 0;
}
19. 动态内存分配
学习如何使用malloc和free来动态分配和释放内存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
printf("Memory successfully allocated at address %d\n", (int)ptr);
for (int i = 0; i < 5; i++)
ptr[i] = i + 1;
printf("Values in the array: ");
for (int i = 0; i < 5; i++)
printf("%d ", ptr[i]);
printf("\n");
free(ptr);
printf("Memory freed.\n");
return 0;
}
20. 文件操作
学习如何使用C语言进行文件操作,如读取和写入文件。
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("File cannot be opened.\n");
exit(0);
}
printf("Content of the file:\n");
while ((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
通过这些实战案例,你可以逐步掌握C语言的基础知识和高级技巧。每个案例都提供了详细的代码和解释,帮助你更好地理解和应用C语言。祝你学习愉快!
