实例1:打印“Hello, World!”
在C语言编程中,第一个实例通常都是打印“Hello, World!”。这个例子简单直观,有助于初学者了解C语言的基本语法。
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
实例2:变量与数据类型
在C语言中,变量用于存储数据。理解不同数据类型对于编写高效的代码至关重要。
#include <stdio.h>
int main() {
int a = 10;
float b = 3.14;
char c = 'A';
printf("整型变量a的值为:%d\n", a);
printf("浮点型变量b的值为:%f\n", b);
printf("字符型变量c的值为:%c\n", c);
return 0;
}
实例3:运算符
C语言提供了丰富的运算符,包括算术运算符、逻辑运算符、位运算符等。
#include <stdio.h>
int main() {
int a = 5, 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 / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
return 0;
}
实例4:控制结构
C语言中的控制结构包括条件语句和循环语句,用于控制程序的执行流程。
#include <stdio.h>
int main() {
int a = 10;
if (a > 5) {
printf("a大于5\n");
} else {
printf("a不大于5\n");
}
for (int i = 1; i <= 5; i++) {
printf("i的值为:%d\n", i);
}
return 0;
}
实例5:函数
函数是C语言中的核心概念,用于组织代码并提高代码的可重用性。
#include <stdio.h>
void sayHello() {
printf("Hello, World!\n");
}
int main() {
sayHello();
return 0;
}
实例6:数组
数组是存储多个相同类型数据的容器,C语言中的数组使用非常广泛。
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("arr[%d]的值为:%d\n", i, arr[i]);
}
return 0;
}
实例7:指针
指针是C语言中非常重要的一种数据类型,用于存储变量的内存地址。
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("a的值为:%d\n", a);
printf("ptr指向的地址为:%p\n", (void *)ptr);
printf("ptr指向的值:%d\n", *ptr);
return 0;
}
实例8:结构体
结构体用于将不同类型的数据组合在一起,形成一个整体。
#include <stdio.h>
struct Student {
char name[50];
int age;
float score;
};
int main() {
struct Student stu1;
strcpy(stu1.name, "张三");
stu1.age = 20;
stu1.score = 90.5;
printf("姓名:%s\n", stu1.name);
printf("年龄:%d\n", stu1.age);
printf("成绩:%f\n", stu1.score);
return 0;
}
实例9:函数指针
函数指针是指向函数的指针,可以用来传递函数作为参数。
#include <stdio.h>
void sayHello() {
printf("Hello, World!\n");
}
int main() {
void (*funcPtr)() = sayHello;
funcPtr();
return 0;
}
实例10:文件操作
C语言提供了丰富的文件操作函数,可以方便地读写文件。
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("打开文件失败\n");
return 1;
}
fprintf(fp, "这是一个示例文件\n");
fclose(fp);
return 0;
}
实例11:动态内存分配
C语言中的动态内存分配可以让我们在运行时分配内存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(10 * sizeof(int));
if (ptr == NULL) {
printf("内存分配失败\n");
return 1;
}
for (int i = 0; i < 10; i++) {
ptr[i] = i;
}
for (int i = 0; i < 10; i++) {
printf("ptr[%d]的值为:%d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
实例12:字符串处理
C语言提供了丰富的字符串处理函数,如strlen、strcpy、strcmp等。
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, World!";
char str2[50] = "Hello, C!";
printf("str1的长度:%lu\n", strlen(str1));
strcpy(str2, str1);
printf("str2的值为:%s\n", str2);
printf("str1和str2是否相等:%d\n", strcmp(str1, str2));
return 0;
}
实例13:排序算法
C语言中的排序算法有很多种,如冒泡排序、选择排序、插入排序等。
#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[] = {5, 2, 8, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("排序后的数组:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
实例14:递归函数
递归函数是一种自己调用自身的函数,C语言中的递归函数可以解决很多问题。
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
printf("num的阶乘:%d\n", factorial(num));
return 0;
}
实例15:链表
链表是一种常用的数据结构,C语言中的链表可以实现动态内存分配。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertNode(struct Node **head, int data) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(struct Node *head) {
struct Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node *head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
insertNode(&head, 4);
insertNode(&head, 5);
printf("链表:");
printList(head);
return 0;
}
实例16:树
树是一种重要的数据结构,C语言中的树可以实现各种复杂的算法。
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode *createNode(int data) {
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void insertNode(struct TreeNode **root, int data) {
if (*root == NULL) {
*root = createNode(data);
} else if (data < (*root)->data) {
insertNode(&((*root)->left), data);
} else {
insertNode(&((*root)->right), data);
}
}
void inorderTraversal(struct TreeNode *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct TreeNode *root = NULL;
insertNode(&root, 5);
insertNode(&root, 3);
insertNode(&root, 7);
insertNode(&root, 2);
insertNode(&root, 4);
insertNode(&root, 6);
insertNode(&root, 8);
printf("中序遍历:");
inorderTraversal(root);
printf("\n");
return 0;
}
实例17:图
图是一种复杂的数据结构,C语言中的图可以用来解决很多实际问题。
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 5
struct Graph {
int numVertices;
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
};
void initializeGraph(struct Graph *graph, int numVertices) {
graph->numVertices = numVertices;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
graph->adjMatrix[i][j] = 0;
}
}
}
void addEdge(struct Graph *graph, int src, int dest) {
graph->adjMatrix[src][dest] = 1;
graph->adjMatrix[dest][src] = 1;
}
void printGraph(struct Graph *graph) {
for (int i = 0; i < graph->numVertices; i++) {
for (int j = 0; j < graph->numVertices; j++) {
printf("%d ", graph->adjMatrix[i][j]);
}
printf("\n");
}
}
int main() {
struct Graph graph;
initializeGraph(&graph, MAX_VERTICES);
addEdge(&graph, 0, 1);
addEdge(&graph, 0, 4);
addEdge(&graph, 1, 2);
addEdge(&graph, 1, 3);
addEdge(&graph, 1, 4);
addEdge(&graph, 2, 3);
addEdge(&graph, 3, 4);
printf("图:\n");
printGraph(&graph);
return 0;
}
实例18:队列
队列是一种先进先出(FIFO)的数据结构,C语言中的队列可以实现动态内存分配。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
void initializeQueue(struct Queue *queue) {
queue->front = 0;
queue->rear = -1;
}
int isEmpty(struct Queue *queue) {
return queue->rear == -1;
}
int isFull(struct Queue *queue) {
return (queue->rear + 1) % MAX_SIZE == queue->front;
}
void enqueue(struct Queue *queue, int item) {
if (isFull(queue)) {
printf("队列已满\n");
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->items[queue->rear] = item;
}
}
int dequeue(struct Queue *queue) {
if (isEmpty(queue)) {
printf("队列已空\n");
return -1;
} else {
int item = queue->items[queue->front];
queue->front = (queue->front + 1) % MAX_SIZE;
return item;
}
}
int main() {
struct Queue queue;
initializeQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
printf("队列:");
while (!isEmpty(&queue)) {
printf("%d ", dequeue(&queue));
}
printf("\n");
return 0;
}
实例19:栈
栈是一种后进先出(LIFO)的数据结构,C语言中的栈可以实现动态内存分配。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
struct Stack {
int items[MAX_SIZE];
int top;
};
void initializeStack(struct Stack *stack) {
stack->top = -1;
}
int isEmpty(struct Stack *stack) {
return stack->top == -1;
}
int isFull(struct Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
void push(struct Stack *stack, int item) {
if (isFull(stack)) {
printf("栈已满\n");
} else {
stack->top++;
stack->items[stack->top] = item;
}
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("栈已空\n");
return -1;
} else {
int item = stack->items[stack->top];
stack->top--;
return item;
}
}
int main() {
struct Stack stack;
initializeStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
printf("栈:");
while (!isEmpty(&stack)) {
printf("%d ", pop(&stack));
}
printf("\n");
return 0;
}
实例20:哈希表
哈希表是一种高效的数据结构,C语言中的哈希表可以实现动态内存分配。
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
struct HashTable {
int *table[TABLE_SIZE];
};
void initializeHashTable(struct HashTable *hashTable) {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable->table[i] = NULL;
}
}
int hashFunction(int key) {
return key % TABLE_SIZE;
}
void insertHashTable(struct HashTable *hashTable, int key) {
int index = hashFunction(key);
if (hashTable->table[index] == NULL) {
hashTable->table[index] = (int *)malloc(sizeof(int));
*hashTable->table[index] = key;
} else {
printf("哈希表中已存在该键值:%d\n", key);
}
}
void printHashTable(struct HashTable *hashTable) {
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable->table[i] != NULL) {
printf("索引:%d,键值:%d\n", i, *hashTable->table[i]);
}
}
}
int main() {
struct HashTable hashTable;
initializeHashTable(&hashTable);
insertHashTable(&hashTable, 10);
insertHashTable(&hashTable, 20);
insertHashTable(&hashTable, 30);
insertHashTable(&hashTable, 40);
insertHashTable(&hashTable, 50);
insertHashTable(&hashTable, 60);
insertHashTable(&hashTable, 70);
insertHashTable(&hashTable, 80);
insertHashTable(&hashTable, 90);
printHashTable(&hashTable);
return 0;
}
实例21:动态规划
动态规划是一种解决优化问题的方法,C语言中的动态规划可以解决很多实际问题。
#include <stdio.h>
int fib(int n) {
if (n <= 1) {
return n;
}
int *dp = (int *)malloc((n + 1) * sizeof(int));
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
int result = dp[n];
free(dp);
return result;
}
int main() {
int n = 10;
printf("fib(%d)的值为:%d\n", n, fib(n));
return 0;
}
实例22:贪心算法
贪心算法是一种局部最优解策略,C语言中的贪心算法可以解决很多实际问题。
”`c
#include
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0,
