1. 打印“Hello, World!”
这是一个最基础的C语言程序,用于输出“Hello, World!”到控制台。
#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. 循环结构
使用循环结构来打印从1到10的数字。
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
4. 分支结构
使用if语句判断一个数是否为偶数。
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
5. 数组操作
本示例展示了如何使用数组来存储和打印数字。
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int i;
printf("Numbers in the array are: ");
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
6. 函数定义和调用
使用函数来计算两个数的最大值。
#include <stdio.h>
int max(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("The maximum number is %d\n", max(a, b));
return 0;
}
7. 字符串操作
使用strlen函数计算字符串的长度。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of string: %lu\n", strlen(str));
return 0;
}
8. 指针基础
使用指针访问和修改变量的值。
#include <stdio.h>
int main() {
int a = 10;
int *ptr;
ptr = &a;
printf("Value of a: %d\n", a);
printf("Value of ptr: %p\n", (void *)ptr);
printf("Value of *ptr: %d\n", *ptr);
*ptr = 20;
printf("Value of a after modification: %d\n", a);
return 0;
}
9. 结构体
使用结构体来存储学生信息。
#include <stdio.h>
typedef struct {
char name[50];
int age;
float gpa;
} Student;
int main() {
Student s1;
strcpy(s1.name, "John Doe");
s1.age = 20;
s1.gpa = 3.5;
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("GPA: %.2f\n", s1.gpa);
return 0;
}
10. 链表
使用链表来存储和打印一系列数字。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void push(Node** head_ref, int new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
push(&head, 1);
push(&head, 2);
push(&head, 3);
push(&head, 4);
push(&head, 5);
printList(head);
return 0;
}
11. 动态内存分配
使用malloc和free函数来动态分配和释放内存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", (ptr + i));
}
printf("You entered: ");
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
free(ptr);
return 0;
}
12. 文件操作
使用fopen和fclose函数来读取和写入文件。
#include <stdio.h>
int main() {
FILE *file;
char filename[] = "example.txt";
char ch;
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
exit(0);
}
printf("Reading characters from file %s\n", filename);
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
return 0;
}
13. 指数函数
使用递归函数计算幂。
#include <stdio.h>
long long int power(int base, int exp) {
if (exp == 0)
return 1;
return base * power(base, exp - 1);
}
int main() {
int base, exp;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exp);
printf("%d^%d = %lld\n", base, exp, power(base, exp));
return 0;
}
14. 排序算法
使用冒泡排序算法对数组进行排序。
#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;
}
15. 链表插入操作
在链表中插入一个新节点。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void insertAtBeginning(Node** head_ref, int new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void insertAtEnd(Node** head_ref, int new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
}
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
insertAtBeginning(&head, 5);
printList(head);
return 0;
}
16. 链表删除操作
从链表中删除一个节点。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void deleteNode(Node** head_ref, int key) {
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(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
insertAtEnd(&head, 5);
printf("Original list: \n");
printList(head);
deleteNode(&head, 3);
printf("List after deleting 3: \n");
printList(head);
return 0;
}
17. 链表反转
反转一个链表。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void reverse(Node** head_ref) {
Node *prev = NULL, *current = *head_ref, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
insertAtEnd(&head, 5);
printf("Original list: \n");
printList(head);
reverse(&head);
printf("Reversed list: \n");
printList(head);
return 0;
}
18. 字符串反转
使用递归函数反转一个字符串。
#include <stdio.h>
#include <string.h>
void reverseString(char* str) {
int len = strlen(str);
if (len <= 1) return;
reverseString(str + 1);
printf("%c", str[len - 1]);
}
int main() {
char str[] = "Hello, World!";
printf("Original string: %s\n", str);
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
19. 检查素数
使用函数检查一个数是否为素数。
#include <stdio.h>
#include <stdbool.h>
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 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;
}
20. 求阶乘
使用递归函数计算一个数的阶乘。
#include <stdio.h>
unsigned long long int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %llu\n", num, factorial(num));
return 0;
}
