实战案例一:C语言基础语法

1.1 变量和数据类型

#include <stdio.h>

int main() {
    int age = 25;
    float salary = 5000.5f;
    char grade = 'A';
    
    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);
    
    return 0;
}

1.2 运算符和表达式

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    int sum = a + b;
    int difference = a - b;
    int product = a * b;
    int quotient = a / b;
    int remainder = a % b;
    
    printf("Sum: %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product: %d\n", product);
    printf("Quotient: %d\n", quotient);
    printf("Remainder: %d\n", remainder);
    
    return 0;
}

实战案例二:控制结构

2.1 条件语句

#include <stdio.h>

int main() {
    int num = 10;
    if (num > 0) {
        printf("Number is positive.\n");
    } else if (num < 0) {
        printf("Number is negative.\n");
    } else {
        printf("Number is zero.\n");
    }
    
    return 0;
}

2.2 循环结构

#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 5; i++) {
        printf("Count: %d\n", i);
    }
    
    return 0;
}

实战案例三:函数和模块化编程

3.1 函数定义和调用

#include <stdio.h>

void sayHello() {
    printf("Hello, World!\n");
}

int main() {
    sayHello();
    return 0;
}

3.2 递归函数

#include <stdio.h>

int factorial(int n) {
    if (n <= 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    int num = 5;
    printf("Factorial of %d is %d\n", num, factorial(num));
    return 0;
}

实战案例四:指针和内存管理

4.1 指针定义和操作

#include <stdio.h>

int main() {
    int a = 10;
    int *ptr = &a;
    
    printf("Value of a: %d\n", a);
    printf("Address of a: %p\n", (void *)&a);
    printf("Value of ptr: %p\n", (void *)ptr);
    printf("Value pointed by ptr: %d\n", *ptr);
    
    return 0;
}

4.2 动态内存分配

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(5 * sizeof(int));
    
    if (ptr != NULL) {
        for (int i = 0; i < 5; i++) {
            ptr[i] = i + 1;
        }
        
        for (int i = 0; i < 5; i++) {
            printf("Value at index %d: %d\n", i, ptr[i]);
        }
        
        free(ptr);
    }
    
    return 0;
}

实战案例五:文件操作

5.1 文件读写

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    
    if (file != NULL) {
        fprintf(file, "Hello, World!\n");
        fclose(file);
    }
    
    file = fopen("example.txt", "r");
    
    if (file != NULL) {
        char buffer[100];
        while (fgets(buffer, sizeof(buffer), file)) {
            printf("%s", buffer);
        }
        fclose(file);
    }
    
    return 0;
}

实战案例六:字符串操作

6.1 字符串拼接

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello, ";
    char str2[] = "World!";
    char result[100];
    
    strcpy(result, str1);
    strcat(result, str2);
    
    printf("Concatenated String: %s\n", result);
    
    return 0;
}

6.2 字符串查找

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char search[] = "World";
    
    int index = strstr(str, search) - str;
    
    printf("Index of '%s' in '%s': %d\n", search, str, index);
    
    return 0;
}

实战案例七:数据结构

7.1 链表

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

void insertAtBeginning(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");
}

int main() {
    Node *head = NULL;
    
    insertAtBeginning(&head, 3);
    insertAtBeginning(&head, 2);
    insertAtBeginning(&head, 1);
    
    printList(head);
    
    return 0;
}

7.2 栈

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 10

typedef struct Stack {
    int items[MAX_SIZE];
    int top;
} Stack;

void initializeStack(Stack *stack) {
    stack->top = -1;
}

int isEmpty(Stack *stack) {
    return stack->top == -1;
}

void push(Stack *stack, int data) {
    if (stack->top < MAX_SIZE - 1) {
        stack->items[++stack->top] = data;
    } else {
        printf("Stack is full.\n");
    }
}

int pop(Stack *stack) {
    if (!isEmpty(stack)) {
        return stack->items[stack->top--];
    } else {
        printf("Stack is empty.\n");
        return -1;
    }
}

int main() {
    Stack stack;
    initializeStack(&stack);
    
    push(&stack, 10);
    push(&stack, 20);
    push(&stack, 30);
    
    printf("Popped: %d\n", pop(&stack));
    printf("Popped: %d\n", pop(&stack));
    printf("Popped: %d\n", pop(&stack));
    
    return 0;
}

实战案例八:网络编程

8.1 创建和监听套接字

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
    
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8080);
    
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    
    if (listen(server_fd, 3) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    
    while ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) > 0) {
        char buffer[1024] = {0};
        read(new_socket, buffer, 1024);
        printf("Message: %s\n", buffer);
        send(new_socket, "Hello from server\n", 18, 0);
        close(new_socket);
    }
    
    if (new_socket < 0) {
        perror("accept");
        exit(EXIT_FAILURE);
    }
    
    return 0;
}

8.2 客户端-服务器通信

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    char buffer[1024] = {0};
    char *hello = "Hello from client";
    
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        printf("\n Socket creation error \n");
        return -1;
    }
    
    memset(&serv_addr, '0', sizeof(serv_addr));
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(8080);
    
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }
    
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("\nConnection Failed \n");
        return -1;
    }
    
    send(sock, hello, strlen(hello), 0);
    read(sock, buffer, 1024);
    printf("Server: %s\n", buffer);
    
    return 0;
}

实战案例九:图形界面编程

9.1 使用GTK创建窗口

#include <gtk/gtk.h>

int main() {
    GtkWidget *window;
    
    gtk_init(NULL, NULL);
    
    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(window);
    
    gtk_main();
    
    return 0;
}

9.2 使用Qt创建窗口

#include <QApplication>
#include <QWidget>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    
    QWidget window;
    window.setWindowTitle("Hello, World!");
    window.resize(200, 200);
    window.show();
    
    return app.exec();
}

实战案例十:多线程编程

10.1 创建和启动线程

#include <stdio.h>
#include <pthread.h>

void *threadFunction(void *arg) {
    printf("Thread started.\n");
    return NULL;
}

int main() {
    pthread_t thread_id;
    
    if (pthread_create(&thread_id, NULL, threadFunction, NULL) != 0) {
        perror("Failed to create thread");
        return 1;
    }
    
    pthread_join(thread_id, NULL);
    
    printf("Thread finished.\n");
    
    return 0;
}

10.2 线程同步

#include <stdio.h>
#include <pthread.h>

int counter = 0;
pthread_mutex_t lock;

void *threadFunction(void *arg) {
    for (int i = 0; i < 1000; i++) {
        pthread_mutex_lock(&lock);
        counter++;
        pthread_mutex_unlock(&lock);
    }
    
    return NULL;
}

int main() {
    pthread_t thread_id1, thread_id2;
    
    pthread_mutex_init(&lock, NULL);
    
    if (pthread_create(&thread_id1, NULL, threadFunction, NULL) != 0) {
        perror("Failed to create thread");
        return 1;
    }
    
    if (pthread_create(&thread_id2, NULL, threadFunction, NULL) != 0) {
        perror("Failed to create thread");
        return 1;
    }
    
    pthread_join(thread_id1, NULL);
    pthread_join(thread_id2, NULL);
    
    printf("Counter: %d\n", counter);
    
    pthread_mutex_destroy(&lock);
    
    return 0;
}

实战案例十一:网络编程

11.1 创建和监听套接字

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
    
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8080);
    
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    
    if (listen(server_fd, 3) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    
    while ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) > 0) {
        char buffer[1024] = {0};
        read(new_socket, buffer, 1024);
        printf("Message: %s\n", buffer);
        send(new_socket, "Hello from server\n", 18, 0);
        close(new_socket);
    }
    
    if (new_socket < 0) {
        perror("accept");
        exit(EXIT_FAILURE);
    }
    
    return 0;
}

11.2 客户端-服务器通信

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    char buffer[1024] = {0};
    char *hello = "Hello from client";
    
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        printf("\n Socket creation error \n");
        return -1;
    }
    
    memset(&serv_addr, '0', sizeof(serv_addr));
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(8080);
    
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }
    
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("\nConnection Failed \n");
        return -1;
    }
    
    send(sock, hello, strlen(hello), 0);
    read(sock, buffer, 1024);
    printf("Server: %s\n", buffer);
    
    return 0;
}

实战案例十二:图形界面编程

12.1 使用GTK创建窗口

#include <gtk/gtk.h>

int main() {
    GtkWidget *window;
    
    gtk_init(NULL, NULL);
    
    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(window);
    
    gtk_main();
    
    return 0;
}

12.2 使用Qt创建窗口

#include <QApplication>
#include <QWidget>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    
    QWidget window;
    window.setWindowTitle("Hello, World!");
    window.resize(200, 200);
    window.show();
    
    return app.exec();
}

实战案例十三:多线程编程

13.1 创建和启动线程

#include <stdio.h>
#include <pthread.h>

void *threadFunction(void *arg) {
    printf("Thread started.\n");
    return NULL;
}

int main() {
    pthread_t thread_id;
    
    if (pthread_create(&thread_id, NULL, threadFunction, NULL) != 0) {
        perror("Failed to create thread");
        return 1;
    }
    
    pthread_join(thread_id, NULL);
    
    printf("Thread finished.\n");
    
    return 0;
}

13.2 线程同步

”`c #include #include

int counter = 0; pthread_mutex_t lock;

void *threadFunction(void *arg) {

for (int i = 0; i < 1000; i++) {
    pthread_mutex_lock(&lock);
    counter++;
    pthread_mutex_unlock(&lock);
}

return NULL;

}

int main() {

pthread_t thread_id1, thread_id2;

pthread_mutex_init(&lock, NULL);

if (pthread_create(&thread_id1, NULL, threadFunction, NULL) != 0) {
    perror("Failed to create thread");
    return 1;
}

if (pthread_create(&thread_id2, NULL, threadFunction, NULL) != 0) {
    perror("Failed to create thread");
    return 1;
}