宁波政企机房,金融级骨干网,优质高端体验、100%性能释放、配备金盾防火墙,可提交工单免费申请ipv6.
携手合作伙伴,实现业务上的双向合作共赢
10+年商誉沉淀,深耕中国香港及海外高端资源
我们为您提供全方位的支持与服务,确保您在使用我们的云服务时无忧无虑。
# C语言经典程序100例
C语言作为一门操作底层硬件、追求效率的经典编程语言,无论是在学术研究、工业开发,还是编程学习入门阶段,都占据着非常重要的位置。本文整理了100个C语言的经典案例,适合不同水平的学习者参考与练习。
## 一、基础算法
1. **求和计算**
输入两个整数并返回它们的和,这是最基础的例子,目的是熟悉输入输出和变量操作。
```c
#include
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a + b);
return 0;
}
if-else的用法。#include
int main() {
int num;
scanf("%d", &num);
if (num % 2 == 0) {
printf("偶数\n");
} else {
printf("奇数\n");
}
return 0;
}
#include
int main() {
int array[10];
int max, i;
for (i = 0; i < 10; i++) {
scanf("%d", &array[i]);
}
max = array[0];
for (i = 1; i < 10; i++) {
if (array[i] > max) {
max = array[i];
}
}
printf("Max value: %d\n", max);
return 0;
}
#include
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 array[5];
int i;
for (i = 0; i < 5; i++) {
scanf("%d", &array[i]);
}
bubbleSort(array, 5);
for (i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
#include
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
swap(&x, &y);
printf("Swapped: %d %d\n", x, y);
return 0;
}
#include
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num;
scanf("%d", &num);
printf("Factorial: %d\n", factorial(num));
return 0;
}
#include
int main() {
FILE *file;
char str[100];
file = fopen("example.txt", "w");
fprintf(file, "Hello, World!\n");
fclose(file);
file = fopen("example.txt", "r");
fscanf(file, "%s", str);
printf("%s\n", str);
fclose(file);
return 0;
}
#include
struct Person {
char name[50];
int age;
};
int main() {
struct Person p1 = {"Alice", 25};
printf("Name: %s, Age: %d\n", p1.name, p1.age);
return 0;
}
#include
#include
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int number;
scanf("%d", &number);
if (isPrime(number)) {
printf("Prime\n");
} else {
printf("Not Prime\n");
}
return 0;
}
#include
#include
struct Node {
int data;
struct Node* next;
};
void append(struct Node** head, int newData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;
newNode->data = newData;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
append(&head, 10);
append(&head, 20);
printList(head);
return 0;
}
上述只是C语言经典100例的精选部分,这些案例涵盖了基础语法、逻辑控制、函数指针、结构体、文件操作和链表等知识点。坚持练习和深入理解每一个案例,提升算法设计与编程能力,在学习C语言的道路上定会受益匪浅。
服务热线:
4009011125电子邮箱:
abcqq@188.comTelegram:
https://t.me/a86cc商务QQ:
3515655888
公众号
微信
Linux工具推荐:
支持一键换源/安装宝塔/1p/系统优化等,运维好帮手!Github开源工具,欢迎star~
https://cb2.cn/helpcontent/230.html
(开源地址:https://github.com/JiaP/cb2cn)
---------------------------------------
邀请好友注册购买可获得高额佣金!