实例1:输出“Hello, World!”
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
这是C语言中最简单的程序,用于输出“Hello, World!”。它演示了如何在屏幕上打印文本。
实例2:变量声明和赋值
#include <stdio.h>
int main() {
int age = 18;
printf("I am %d years old.\n", age);
return 0;
}
在这个例子中,我们声明了一个整型变量age,并给它赋值为18。然后,我们使用printf函数打印出年龄。
实例3:算术运算
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
printf("The sum of %d and %d is %d.\n", num1, num2, sum);
return 0;
}
这个实例演示了如何进行基本的算术运算,并将结果输出到屏幕。
实例4:控制流 - if语句
#include <stdio.h>
int main() {
int age = 18;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
这个例子展示了如何使用if语句进行条件判断。
实例5:控制流 - switch语句
#include <stdio.h>
int main() {
int day = 3;
switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day.\n");
}
return 0;
}
这个例子使用了switch语句来根据不同的值执行不同的代码块。
实例6:循环 - for循环
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
这个例子演示了如何使用for循环打印从1到5的数字。
实例7:循环 - while循环
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
这个例子使用了while循环来实现同样的功能。
实例8:数组声明和访问
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
printf("The first number is %d.\n", numbers[0]);
return 0;
}
这个例子展示了如何声明一个数组,并访问其元素。
实例9:函数定义和调用
#include <stdio.h>
void sayHello() {
printf("Hello!\n");
}
int main() {
sayHello();
return 0;
}
在这个例子中,我们定义了一个名为sayHello的函数,并在main函数中调用它。
实例10:结构体定义和访问
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person person;
strcpy(person.name, "John Doe");
person.age = 30;
printf("Name: %s, Age: %d\n", person.name, person.age);
return 0;
}
这个例子展示了如何定义和使用结构体。
实例11:指针和地址
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("The value of num is %d.\n", num);
printf("The address of num is %p.\n", (void *)ptr);
printf("The value of *ptr is %d.\n", *ptr);
return 0;
}
这个例子演示了如何使用指针和地址。
实例12:动态内存分配
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d\n", ptr[i]);
}
free(ptr);
return 0;
}
这个例子展示了如何使用malloc函数动态分配内存,并在使用完毕后释放它。
实例13:字符串操作 - strlen
#include <stdio.h>
#include <string.h>
int main() {
char str[100] = "Hello, World!";
printf("The length of the string is %ld.\n", strlen(str));
return 0;
}
这个例子演示了如何使用strlen函数来获取字符串的长度。
实例14:字符串操作 - strcpy
#include <stdio.h>
#include <string.h>
int main() {
char source[100] = "Hello, World!";
char destination[100];
strcpy(destination, source);
printf("The destination string is %s.\n", destination);
return 0;
}
这个例子展示了如何使用strcpy函数将一个字符串复制到另一个字符串中。
实例15:字符串操作 - strcat
#include <stdio.h>
#include <string.h>
int main() {
char source[100] = "Hello, ";
char destination[100] = "World!";
strcat(source, destination);
printf("The concatenated string is %s.\n", source);
return 0;
}
这个例子展示了如何使用strcat函数将一个字符串连接到另一个字符串的末尾。
实例16:字符串操作 - strstr
#include <stdio.h>
#include <string.h>
int main() {
char source[100] = "Hello, World!";
char search[50] = "World";
char *result = strstr(source, search);
if (result != NULL) {
printf("The substring is found at index %ld.\n", result - source);
} else {
printf("The substring is not found.\n");
}
return 0;
}
这个例子展示了如何使用strstr函数在字符串中查找子字符串。
实例17:字符串操作 - strtok
#include <stdio.h>
#include <string.h>
int main() {
char str[100] = "Hello, World!";
char *token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
return 0;
}
这个例子展示了如何使用strtok函数将一个字符串分割成多个子字符串。
实例18:文件操作 - 打开文件
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("File opening failed.\n");
return 1;
}
// 读取文件内容
fclose(file);
return 0;
}
这个例子演示了如何使用fopen函数打开一个文件。
实例19:文件操作 - 读取文件内容
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("File opening failed.\n");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
这个例子展示了如何使用fgets函数读取文件内容。
实例20:文件操作 - 写入文件
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("File opening failed.\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
这个例子展示了如何使用fprintf函数将文本写入文件。
实例21:文件操作 - 检查文件是否存在
#include <stdio.h>
#include <sys/stat.h>
int main() {
struct stat buffer;
if (stat("example.txt", &buffer) == 0) {
printf("File exists.\n");
} else {
printf("File does not exist.\n");
}
return 0;
}
这个例子展示了如何使用stat函数检查文件是否存在。
实例22:进程控制 - 创建子进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// 子进程
printf("I am the child process.\n");
} else {
// 父进程
printf("I am the parent process, child PID: %d\n", pid);
}
return 0;
}
这个例子展示了如何使用fork函数创建一个子进程。
实例23:进程控制 - 等待子进程结束
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// 子进程
printf("I am the child process.\n");
_exit(0);
} else {
// 父进程
int status;
waitpid(pid, &status, 0);
printf("Child process exited with status %d.\n", status);
}
return 0;
}
这个例子展示了如何使用waitpid函数等待子进程结束。
实例24:进程控制 - 发送信号
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void signalHandler(int signum) {
printf("Received signal %d\n", signum);
}
int main() {
signal(SIGINT, signalHandler);
printf("Press Ctrl+C to receive a signal.\n");
while (1) {
pause();
}
return 0;
}
这个例子展示了如何使用signal函数注册信号处理函数。
实例25:进程控制 - 管道
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { // 子进程
close(pipefd[1]); // 关闭未使用的管道写入端
dup2(pipefd[0], STDIN_FILENO); // 将管道读取端复制到标准输入
char buffer[100];
while (fgets(buffer, sizeof(buffer), stdin)) {
printf("Echoing: %s", buffer);
}
exit(EXIT_SUCCESS);
} else { // 父进程
close(pipefd[0]); // 关闭未使用的管道读取端
dup2(pipefd[1], STDOUT_FILENO); // 将管道写入端复制到标准输出
char *args[] = {"./child", NULL};
execvp("./child", args);
perror("execvp");
exit(EXIT_FAILURE);
}
}
这个例子展示了如何使用管道进行进程间通信。
实例26:多线程 - 创建线程
#include <stdio.h>
#include <pthread.h>
void *threadFunction(void *arg) {
printf("Thread ID: %ld\n", pthread_self());
return NULL;
}
int main() {
pthread_t thread_id;
if (pthread_create(&thread_id, NULL, threadFunction, NULL) != 0) {
perror("pthread_create");
return 1;
}
pthread_join(thread_id, NULL);
return 0;
}
这个例子展示了如何使用pthread_create函数创建一个线程。
实例27:多线程 - 线程同步
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t lock;
void *threadFunction(void *arg) {
pthread_mutex_lock(&lock);
printf("Thread ID: %ld\n", pthread_self());
pthread_mutex_unlock(&lock);
return NULL;
}
int main() {
pthread_t thread_id;
pthread_mutex_init(&lock, NULL);
pthread_create(&thread_id, NULL, threadFunction, NULL);
pthread_join(thread_id, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
这个例子展示了如何使用互斥锁进行线程同步。
实例28:网络编程 - 创建套接字
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
printf("Socket created with file descriptor %d.\n", sockfd);
close(sockfd);
return 0;
}
这个例子展示了如何使用socket函数创建一个套接字。
实例29:网络编程 - 连接到服务器
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr.s_addr = inet_addr("www.google.com");
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}
printf("Connected to server.\n");
close(sockfd);
return 0;
}
这个例子展示了如何使用connect函数连接到服务器。
实例30:网络编程 - 发送和接收数据
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr.s_addr = inet_addr("www.google.com");
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}
char buffer[1024];
read(sockfd, buffer, sizeof(buffer));
printf("Received data: %s\n", buffer);
write(sockfd, "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n", 59);
read(sockfd, buffer, sizeof(buffer));
printf("Received data: %s\n", buffer);
close(sockfd);
return 0;
}
这个例子展示了如何使用read和write函数发送和接收数据。
实例31:图形用户界面 - 创建窗口
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello, World!");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
这个例子展示了如何使用GTK创建一个简单的图形用户界面窗口。
实例32:图形用户界面 - 添加按钮
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window, *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello, World!");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
button = gtk_button_new_with_label("Click me!");
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
这个例子展示了如何使用GTK添加一个按钮到窗口。
实例33:图形用户界面 - 事件处理
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
static void close_window(GtkWidget *widget, gpointer data) {
gtk_main_quit();
}
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello, World!");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
GtkWidget *button = gtk_button_new_with_label("Close");
g_signal_connect(button, "clicked", G_CALLBACK(close_window), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
这个例子展示了如何使用GTK处理按钮点击事件。
实例34:图形用户界面 - 使用布局管理器
”`c
#include
int main(int
