实例1:变量作用域与生命周期
在C语言中,变量的作用域和生命周期是基础中的基础。以下是一个关于变量作用域和生命周期的实例:
#include <stdio.h>
int main() {
int a = 10; // 全局变量
{
int b = 20; // 局部变量
printf("局部变量b的值:%d\n", b);
}
printf("全局变量a的值:%d\n", a);
return 0;
}
在这个例子中,变量a是全局变量,其作用域是整个文件,而变量b是局部变量,其作用域仅在花括号{}内。
实例2:指针与数组
指针和数组是C语言中非常重要的概念。以下是一个关于指针和数组的实例:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // 指针指向数组首地址
for (int i = 0; i < 5; i++) {
printf("数组元素:%d\n", *(ptr + i));
}
return 0;
}
在这个例子中,我们通过指针访问数组元素,演示了指针和数组之间的关系。
实例3:函数参数传递
函数参数传递是C语言编程中常见的问题。以下是一个关于函数参数传递的实例:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
在这个例子中,我们通过指针传递变量的地址来实现两个变量值的交换。
实例4:结构体与联合体
结构体和联合体是C语言中用于组织复杂数据的结构。以下是一个关于结构体和联合体的实例:
#include <stdio.h>
typedef struct {
int id;
char name[50];
} Student;
typedef union {
int id;
char name[50];
} Union;
int main() {
Student stu = {1, "Alice"};
Union u = {2, "Bob"};
printf("结构体成员id:%d, name:%s\n", stu.id, stu.name);
printf("联合体成员id:%d, name:%s\n", u.id, u.name);
return 0;
}
在这个例子中,我们分别定义了结构体和联合体,并演示了它们的使用方法。
实例5:文件操作
文件操作是C语言编程中常见的任务。以下是一个关于文件操作的实例:
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
perror("打开文件失败");
return 1;
}
fprintf(fp, "Hello, World!\n");
fclose(fp);
fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("打开文件失败");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
fclose(fp);
return 0;
}
在这个例子中,我们演示了如何使用C语言进行文件读写操作。
实例6:动态内存分配
动态内存分配是C语言编程中常用的技术。以下是一个关于动态内存分配的实例:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
perror("内存分配失败");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("数组元素:%d\n", arr[i]);
}
free(arr);
return 0;
}
在这个例子中,我们演示了如何使用malloc和free函数进行动态内存分配和释放。
实例7:链表操作
链表是C语言中常用的数据结构。以下是一个关于链表操作的实例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert(Node **head, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(Node *head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
void freeList(Node *head) {
Node *temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
Node *head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
printList(head);
freeList(head);
return 0;
}
在这个例子中,我们演示了如何使用链表进行插入、打印和释放操作。
实例8:递归函数
递归函数是C语言中一种强大的编程技巧。以下是一个关于递归函数的实例:
#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;
}
在这个例子中,我们使用递归函数计算阶乘。
实例9:字符串处理
字符串处理是C语言编程中常见的任务。以下是一个关于字符串处理的实例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[100] = "World";
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
printf("Length of str1: %lu\n", strlen(str1));
return 0;
}
在这个例子中,我们演示了字符串连接和长度计算。
实例10:时间处理
时间处理是C语言编程中常见的任务。以下是一个关于时间处理的实例:
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Current time: %s", asctime(timeinfo));
return 0;
}
在这个例子中,我们演示了如何获取和打印当前时间。
实例11:随机数生成
随机数生成是C语言编程中常见的任务。以下是一个关于随机数生成的实例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
for (int i = 0; i < 10; i++) {
printf("%d ", rand());
}
printf("\n");
return 0;
}
在这个例子中,我们演示了如何生成随机数。
实例12:位操作
位操作是C语言中一种高效的编程技巧。以下是一个关于位操作的实例:
#include <stdio.h>
int main() {
int a = 5; // 二进制:101
int b = 3; // 二进制:011
printf("a & b: %d\n", a & b); // 与操作:001
printf("a | b: %d\n", a | b); // 或操作:111
printf("a ^ b: %d\n", a ^ b); // 异或操作:110
printf("a << 1: %d\n", a << 1); // 左移操作:1010
printf("a >> 1: %d\n", a >> 1); // 右移操作:10
return 0;
}
在这个例子中,我们演示了位操作的应用。
实例13:文件包含
文件包含是C语言中常用的预处理指令。以下是一个关于文件包含的实例:
// header.h
#ifndef HEADER_H
#define HEADER_H
void printMessage();
#endif
// main.c
#include "header.h"
void printMessage() {
printf("Hello, World!\n");
}
int main() {
printMessage();
return 0;
}
在这个例子中,我们使用文件包含指令将header.h文件包含到main.c文件中。
实例14:宏定义
宏定义是C语言中一种强大的预处理指令。以下是一个关于宏定义的实例:
#include <stdio.h>
#define PI 3.14159
int main() {
printf("PI: %f\n", PI);
return 0;
}
在这个例子中,我们使用宏定义定义了一个常量PI。
实例15:条件编译
条件编译是C语言中一种强大的预处理指令。以下是一个关于条件编译的实例:
#include <stdio.h>
#ifdef DEBUG
#define DEBUG_MODE
#endif
int main() {
#ifdef DEBUG_MODE
printf("Debug mode is enabled.\n");
#endif
return 0;
}
在这个例子中,我们使用条件编译指令在调试模式下输出一条消息。
实例16:预处理器指令
预处理器指令是C语言中一种强大的预处理指令。以下是一个关于预处理器指令的实例:
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main() {
int x = 5, y = 10;
printf("Max of %d and %d is %d\n", x, y, MAX(x, y));
return 0;
}
在这个例子中,我们使用预处理器指令定义了一个宏MAX来计算两个数的最大值。
实例17:结构体数组
结构体数组是C语言中一种常用的数据结构。以下是一个关于结构体数组的实例:
#include <stdio.h>
typedef struct {
int id;
char name[50];
} Student;
int main() {
Student students[3] = {
{1, "Alice"},
{2, "Bob"},
{3, "Charlie"}
};
for (int i = 0; i < 3; i++) {
printf("Student %d: %s\n", students[i].id, students[i].name);
}
return 0;
}
在这个例子中,我们定义了一个结构体数组students,并演示了如何访问和打印数组元素。
实例18:结构体指针
结构体指针是C语言中一种强大的编程技巧。以下是一个关于结构体指针的实例:
#include <stdio.h>
typedef struct {
int id;
char name[50];
} Student;
void printStudent(Student *stu) {
printf("Student ID: %d, Name: %s\n", stu->id, stu->name);
}
int main() {
Student stu = {1, "Alice"};
printStudent(&stu);
return 0;
}
在这个例子中,我们使用结构体指针访问和打印学生信息。
实例19:联合体
联合体是C语言中一种特殊的数据结构。以下是一个关于联合体的实例:
#include <stdio.h>
typedef union {
int id;
char name[50];
} Union;
int main() {
Union u;
u.id = 1;
printf("Union ID: %d\n", u.id);
u.name[0] = 'A';
u.name[1] = 'l';
u.name[2] = 'i';
u.name[3] = 'c';
u.name[4] = '\0';
printf("Union Name: %s\n", u.name);
return 0;
}
在这个例子中,我们演示了联合体的使用方法。
实例20:枚举类型
枚举类型是C语言中一种用于定义一组命名的整数的类型。以下是一个关于枚举类型的实例:
#include <stdio.h>
typedef enum {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Weekday;
int main() {
Weekday today = FRIDAY;
printf("Today is %d\n", today);
return 0;
}
在这个例子中,我们使用枚举类型定义了一周中的每一天。
实例21:静态变量
静态变量是C语言中一种具有局部作用域的变量,其值在函数调用之间保持不变。以下是一个关于静态变量的实例:
#include <stdio.h>
void printCount() {
static int count = 0;
count++;
printf("Count: %d\n", count);
}
int main() {
printCount();
printCount();
printCount();
return 0;
}
在这个例子中,我们使用静态变量count来跟踪函数调用的次数。
实例22:外部变量
外部变量是C语言中一种具有全局作用域的变量,其值可以在程序的不同部分被访问和修改。以下是一个关于外部变量的实例:
#include <stdio.h>
int globalVar = 10;
void printGlobalVar() {
printf("Global variable: %d\n", globalVar);
}
int main() {
printGlobalVar();
return 0;
}
在这个例子中,我们定义了一个外部变量globalVar,并在函数printGlobalVar中访问它。
实例23:全局变量
全局变量是C语言中一种具有全局作用域的变量,其值可以在程序的不同部分被访问和修改。以下是一个关于全局变量的实例:
#include <stdio.h>
int globalVar = 10;
void modifyGlobalVar() {
globalVar = 20;
}
int main() {
printf("Before modifyGlobalVar: %d\n", globalVar);
modifyGlobalVar();
printf("After modifyGlobalVar: %d\n", globalVar);
return 0;
}
在这个例子中,我们定义了一个全局变量globalVar,并在函数modifyGlobalVar中修改了它的值。
实例24:函数指针
函数指针是C语言中一种指向函数的指针。以下是一个关于函数指针的实例:
#include <stdio.h>
void printMessage() {
printf("Hello, World!\n");
}
int main() {
void (*funcPtr)() = printMessage;
funcPtr();
return 0;
}
在这个例子中,我们定义了一个函数指针funcPtr,并将其指向printMessage函数。
实例25:回调函数
回调函数是C语言中一种常见的编程技巧。以下是一个关于回调函数的实例:
#include <stdio.h>
void printMessage(const char *message) {
printf("%s\n", message);
}
void processMessage(const char *message, void (*callback)(const char *)) {
callback(message);
}
int main() {
processMessage("Hello, World!", printMessage);
return 0;
}
在这个例子中,我们使用回调函数printMessage来处理传入的消息。
实例26:宏定义函数
宏定义函数是C语言中一种常用的预处理指令。以下是一个关于宏定义函数的实例:
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main() {
int x = 5, y = 10;
printf("Max of %d and %d is %d\n", x, y, MAX(x, y));
return 0;
}
在这个例子中,我们使用宏定义函数MAX来计算两个数的最大值。
实例27:结构体与函数
结构体与函数是C语言中一种常见的组合。以下是一个关于结构体与函数的实例:
#include <stdio.h>
typedef struct {
int id;
char name[50];
} Student;
void printStudent(Student stu) {
printf("Student ID: %d, Name: %s\n", stu.id, stu.name);
}
int main() {
Student stu = {1, "Alice"};
printStudent(stu);
return 0;
}
在这个例子中,我们定义了一个结构体Student,并使用函数printStudent来打印学生信息。
实例28:联合体与函数
联合体与函数是C语言中一种常见的组合。以下是一个关于联合体与函数的实例:
#include <stdio.h>
typedef union {
int id;
char name[50];
} Union;
void printUnion(Union u) {
printf("Union ID: %d\n", u.id);
printf("Union Name: %s\n", u.name);
}
int main() {
Union u;
u.id = 1;
printUnion(u);
u.name[0] = 'A';
u.name[1] = 'l';
u.name[2] = 'i';
u.name[3] = 'c';
u.name[4] = '\0';
printUnion(u);
return 0;
}
在这个例子中,我们定义了一个联合体Union,并使用函数printUnion来打印联合体成员。
实例29:枚举类型与函数
枚举类型与函数是C语言中一种常见的组合。以下是一个关于枚举类型与函数的实例:
”`c
#include
typedef enum {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Weekday;
void printWeekday(Weekday weekday) {
switch (weekday) {
case MONDAY:
