以下是十個經典的C語言程序示例,這些程序涵蓋了從基礎到稍復雜的應用場景,適合初學者和有一定基礎的開發者學習和參考。
1. Hello, World!
這是每個初學者學習編程時的第一個程序,用于驗證開發環境是否正確配置。
#include <stdio.h>int main() {printf("Hello, World!\n");return 0;
}
2. 求兩個數的最大公約數
使用歐幾里得算法計算兩個整數的最大公約數。
#include <stdio.h>int gcd(int a, int b) {while (b != 0) {int temp = b;b = a % b;a = temp;}return a;
}int main() {int num1, num2;printf("Enter two integers: ");scanf("%d %d", &num1, &num2);printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));return 0;
}
3. 素數判斷
判斷一個數是否為素數。
#include <stdio.h>
#include <math.h>int isPrime(int num) {if (num <= 1) return 0;for (int i = 2; i <= sqrt(num); i++) {if (num % i == 0) return 0;}return 1;
}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;
}
4. 冒泡排序
對數組進行冒泡排序。
#include <stdio.h>void bubbleSort(int arr[], int n) {for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - i - 1; j++) {if (arr[j] > arr[j + 1]) {int 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;
}
5. 斐波那契數列
生成斐波那契數列的前N項。
#include <stdio.h>void fibonacci(int n) {int a = 0, b = 1, c;printf("Fibonacci sequence: \n");for (int i = 0; i < n; i++) {printf("%d ", a);c = a + b;a = b;b = c;}
}int main() {int n;printf("Enter the number of terms: ");scanf("%d", &n);fibonacci(n);return 0;
}
6. 字符串反轉
反轉一個字符串。
#include <stdio.h>
#include <string.h>void reverseString(char str[]) {int len = strlen(str);for (int i = 0; i < len / 2; i++) {char temp = str[i];str[i] = str[len - i - 1];str[len - i - 1] = temp;}
}int main() {char str[100];printf("Enter a string: ");scanf("%99s", str);reverseString(str);printf("Reversed string: %s\n", str);return 0;
}
7. 二分查找
在有序數組中查找一個元素。
#include <stdio.h>int binarySearch(int arr[], int l, int r, int x) {while (l <= r) {int m = l + (r - l) / 2;if (arr[m] == x) return m;if (arr[m] < x) l = m + 1;else r = m - 1;}return -1;
}int main() {int arr[] = {2, 3, 4, 10, 40};int n = sizeof(arr) / sizeof(arr[0]);int x = 10;int result = binarySearch(arr, 0, n - 1, x);if (result == -1) {printf("Element is not present in array\n");} else {printf("Element is present at index %d\n", result);}return 0;
}
8. 矩陣乘法
計算兩個矩陣的乘積。
#include <stdio.h>void multiplyMatrices(int mat1[][10], int mat2[][10], int res[][10], int r1, int c1, int r2, int c2) {for (int i = 0; i < r1; i++) {for (int j = 0; j < c2; j++) {res[i][j] = 0;for (int k = 0; k < c1; k++) {res[i][j] += mat1[i][k] * mat2[k][j];}}}
}int main() {int mat1[10][10], mat2[10][10], res[10][10];int r1, c1, r2, c2;printf("Enter rows and columns for first matrix: ");scanf("%d %d", &r1, &c1);printf("Enter rows and columns for second matrix: ");scanf("%d %d", &r2, &c2);if (c1 != r2) {printf("Matrix multiplication not possible.\n");return 0;}printf("Enter elements of first matrix:\n");for (int i = 0; i < r1; i++) {for (int j = 0; j < c1; j++) {scanf("%d", &mat1[i][j]);}}printf("Enter elements of second matrix:\n");for (int i = 0; i < r2; i++) {for (int j = 0; j < c2; j++) {scanf("%d", &mat2[i][j]);}}multiplyMatrices(mat1, mat2, res, r1, c1, r2, c2);printf("Resultant matrix:\n");for (int i = 0; i < r1; i++) {for (int j = 0; j < c2; j++) {printf("%d ", res[i][j]);}printf("\n");}return 0;
}
9. 鏈表反轉
反轉一個單鏈表。
#include <stdio.h>
#include <stdlib.h>struct Node {int data;struct Node* next;
};struct Node* newNode(int data) {struct Node* node = (struct Node*)malloc(sizeof(struct Node));node->data = data;node->next = NULL;return node;
}void reverse(struct Node** head) {struct Node* prev = NULL;struct Node* current = *head;struct Node* next = NULL;while (current != NULL) {next = current->next;current->next = prev;prev = current;current = next;}*head = prev;
}void printList(struct Node* node) {while (node != NULL) {printf("%d ", node->data);node = node->next;}printf("\n");
}int main() {struct Node* head = newNode(1);head->next = newNode(2);head->next->next = newNode(3);head->next->next->next = newNode(4);head->next->next->next->next = newNode(5);printf("Original list: ");printList(head);reverse(&head);printf("Reversed list: ");printList(head);return 0;
}
10. 文件讀寫
讀取文件內容并寫入到另一個文件。
#include <stdio.h>int main() {FILE* sourceFile = fopen("source.txt", "r");FILE* targetFile = fopen("target.txt", "w");