实例1:打印“Hello, World!”
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
这是一个简单的C语言程序,用于打印“Hello, World!”到控制台。
实例2:变量声明和赋值
#include <stdio.h>
int main() {
int a = 10;
printf("The value of a is: %d\n", a);
return 0;
}
在这个例子中,我们声明了一个整型变量a,并将其赋值为10。
实例3:使用if语句进行条件判断
#include <stdio.h>
int main() {
int a = 10;
if (a > 5) {
printf("a is greater than 5\n");
}
return 0;
}
这个实例展示了如何使用if语句进行条件判断。
实例4:使用for循环打印数字序列
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
这个例子使用for循环打印从1到10的数字序列。
实例5:使用while循环计算阶乘
#include <stdio.h>
int main() {
int n, i, fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
while (i <= n) {
fact = fact * i;
i++;
}
printf("Factorial of %d is %d\n", n, fact);
return 0;
}
这个实例使用while循环计算给定整数的阶乘。
实例6:使用switch语句进行多分支选择
#include <stdio.h>
int main() {
int a;
printf("Enter an integer: ");
scanf("%d", &a);
switch (a) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Other\n");
break;
}
return 0;
}
这个例子展示了如何使用switch语句进行多分支选择。
实例7:使用数组存储和访问数据
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("The 3rd element of the array is %d\n", arr[2]);
return 0;
}
这个例子展示了如何使用数组存储和访问数据。
实例8:使用字符串处理函数
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[100] = "World";
printf("Concatenated string: %s\n", strcat(str1, str2));
return 0;
}
这个例子展示了如何使用字符串处理函数strcat将两个字符串连接起来。
实例9:使用函数进行模块化编程
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet();
return 0;
}
这个例子展示了如何使用函数进行模块化编程。
实例10:使用指针访问内存地址
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("The value of a is %d\n", *ptr);
return 0;
}
这个例子展示了如何使用指针访问内存地址。
实例11:使用结构体存储复杂数据
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person person;
strcpy(person.name, "John Doe");
person.age = 25;
printf("Name: %s, Age: %d\n", person.name, person.age);
return 0;
}
这个例子展示了如何使用结构体存储复杂数据。
实例12:使用文件操作读写数据
#include <stdio.h>
int main() {
FILE *file;
char str[100];
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "This is a test file.\n");
fclose(file);
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fscanf(file, "%s", str);
printf("Read from file: %s\n", str);
fclose(file);
return 0;
}
这个例子展示了如何使用文件操作读写数据。
实例13:使用动态内存分配
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}
这个例子展示了如何使用动态内存分配。
实例14:使用函数指针
#include <stdio.h>
int add(int x, int y) {
return x + y;
}
int main() {
int (*ptr)(int, int) = add;
printf("The sum is: %d\n", ptr(3, 4));
return 0;
}
这个例子展示了如何使用函数指针。
实例15:使用宏定义
#include <stdio.h>
#define PI 3.14159
int main() {
printf("The value of PI is: %f\n", PI);
return 0;
}
这个例子展示了如何使用宏定义。
实例16:使用预处理指令
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int arr[MAX_SIZE];
printf("The size of the array is: %d\n", MAX_SIZE);
return 0;
}
这个例子展示了如何使用预处理指令。
实例17:使用预处理指令条件编译
#include <stdio.h>
#ifdef DEBUG
#define DEBUG_MODE
#endif
int main() {
#ifdef DEBUG_MODE
printf("Debug mode is enabled.\n");
#endif
return 0;
}
这个例子展示了如何使用预处理指令条件编译。
实例18:使用位运算符
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
return 0;
}
这个例子展示了如何使用位运算符。
实例19:使用共用体
#include <stdio.h>
union Data {
int i;
float f;
char c[4];
};
int main() {
union Data d;
d.i = 10;
printf("d.i = %d\n", d.i);
d.f = 10.5;
printf("d.f = %f\n", d.f);
printf("d.c = %s\n", d.c);
return 0;
}
这个例子展示了如何使用共用体。
实例20:使用枚举类型
#include <stdio.h>
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main() {
enum Weekday day = Wednesday;
printf("The day is: %d\n", day);
return 0;
}
这个例子展示了如何使用枚举类型。
实例21:使用结构体数组
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student students[3] = {
{"Alice", 20},
{"Bob", 22},
{"Charlie", 24}
};
for (int i = 0; i < 3; i++) {
printf("Name: %s, Age: %d\n", students[i].name, students[i].age);
}
return 0;
}
这个例子展示了如何使用结构体数组。
实例22:使用链表
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = NULL, *second = NULL, *third = NULL;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printf("The linked list is: ");
struct Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
return 0;
}
这个例子展示了如何使用链表。
实例23:使用递归函数
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int n = 5;
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}
这个例子展示了如何使用递归函数计算阶乘。
实例24:使用队列
#include <stdio.h>
#include <stdlib.h>
#define QUEUE_SIZE 5
struct Queue {
int items[QUEUE_SIZE];
int front;
int rear;
};
void initializeQueue(struct Queue *q) {
q->front = 0;
q->rear = -1;
}
int isEmpty(struct Queue *q) {
return q->front > q->rear;
}
void enqueue(struct Queue *q, int item) {
if ((q->rear + 1) % QUEUE_SIZE == q->front) {
printf("Queue is full\n");
} else {
q->rear = (q->rear + 1) % QUEUE_SIZE;
q->items[q->rear] = item;
}
}
int dequeue(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
} else {
int item = q->items[q->front];
q->front = (q->front + 1) % QUEUE_SIZE;
return item;
}
}
int main() {
struct Queue q;
initializeQueue(&q);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
enqueue(&q, 4);
enqueue(&q, 5);
printf("Dequeued: %d\n", dequeue(&q));
printf("Dequeued: %d\n", dequeue(&q));
printf("Dequeued: %d\n", dequeue(&q));
printf("Dequeued: %d\n", dequeue(&q));
printf("Dequeued: %d\n", dequeue(&q));
return 0;
}
这个例子展示了如何使用队列。
实例25:使用栈
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 5
struct Stack {
int items[STACK_SIZE];
int top;
};
void initializeStack(struct Stack *s) {
s->top = -1;
}
int isEmpty(struct Stack *s) {
return s->top == -1;
}
void push(struct Stack *s, int item) {
if (s->top == STACK_SIZE - 1) {
printf("Stack is full\n");
} else {
s->top++;
s->items[s->top] = item;
}
}
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
} else {
int item = s->items[s->top];
s->top--;
return item;
}
}
int main() {
struct Stack s;
initializeStack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
push(&s, 4);
push(&s, 5);
printf("Popped: %d\n", pop(&s));
printf("Popped: %d\n", pop(&s));
printf("Popped: %d\n", pop(&s));
printf("Popped: %d\n", pop(&s));
printf("Popped: %d\n", pop(&s));
return 0;
}
这个例子展示了如何使用栈。
实例26:使用排序算法
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
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]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
这个例子展示了如何使用冒泡排序算法对数组进行排序。
实例27:使用查找算法
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
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;
} else 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;
printf("Element is present at index %d\n", linearSearch(arr, n, x));
printf("Element is present at index %d\n", binarySearch(arr, 0, n - 1, x));
return 0;
}
这个例子展示了如何使用线性查找和二分查找算法查找数组中的元素。
实例28:使用动态规划
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n = 10;
printf("Fibonacci series: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
这个例子展示了如何使用动态规划计算斐波那契数列。
实例29:使用贪心算法
#include <stdio.h>
void maximumSumSubarray(int arr[], int n) {
int max_sum = arr[0], curr_sum = arr[0];
for (int i = 1; i < n; i++) {
curr_sum = (arr[i] > curr_sum + arr[i]) ? arr[i] : curr_sum + arr[i];
max_sum = (max_sum > curr_sum) ? max_sum : curr_sum;
}
printf("Maximum subarray sum is %d\n", max_sum);
}
int main() {
int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(arr) / sizeof(arr[0]);
maximumSumSubarray(arr, n);
return 0;
}
这个例子展示了如何使用贪心算法计算最大子数组和。
实例30:使用分治算法
”`c
#include
int merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2
