引言

C语言作为一门历史悠久且应用广泛的编程语言,对于编程初学者来说,掌握C语言的基本语法和编程思维至关重要。为了帮助初学者巩固C语言基础,提升编程能力,本文将提供100道趣味C语言编程题目,涵盖基础语法、数据结构、算法等方面,旨在通过实践提高编程水平。

一、基础语法题(1-20题)

1. 变量和数据类型

编写一个C程序,定义一个整型变量age,并赋值为25,然后打印出age的值。

#include <stdio.h>

int main() {
    int age = 25;
    printf("Age: %d\n", age);
    return 0;
}

2. 运算符

编写一个C程序,计算并打印表达式5 + 3 * 2 / (1 - 4)的值。

#include <stdio.h>

int main() {
    printf("Result: %d\n", 5 + 3 * 2 / (1 - 4));
    return 0;
}

3. 条件语句

编写一个C程序,判断变量x的值,如果大于0,则打印“Positive”,否则打印“Negative”。

#include <stdio.h>

int main() {
    int x = 10;
    if (x > 0) {
        printf("Positive\n");
    } else {
        printf("Negative\n");
    }
    return 0;
}

4. 循环语句

编写一个C程序,使用for循环打印1到10的整数。

#include <stdio.h>

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

5. 数组

编写一个C程序,定义一个整型数组numbers,包含5个元素,并初始化为{1, 2, 3, 4, 5},然后打印出数组中的每个元素。

#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        printf("%d\n", numbers[i]);
    }
    return 0;
}

6. 函数

编写一个C程序,定义一个函数sum,计算两个整数的和,并在主函数中调用该函数。

#include <stdio.h>

int sum(int a, int b) {
    return a + b;
}

int main() {
    int a = 10, b = 20;
    printf("Sum: %d\n", sum(a, b));
    return 0;
}

7. 字符串

编写一个C程序,定义一个字符串str,包含“Hello, World!”,然后打印出字符串中的每个字符。

#include <stdio.h>

int main() {
    char str[] = "Hello, World!";
    for (int i = 0; str[i] != '\0'; i++) {
        printf("%c\n", str[i]);
    }
    return 0;
}

8. 指针

编写一个C程序,定义一个整型变量num,并使用指针访问和修改该变量的值。

#include <stdio.h>

int main() {
    int num = 10;
    int *ptr = &num;
    printf("Original value: %d\n", *ptr);
    *ptr = 20;
    printf("Modified value: %d\n", *ptr);
    return 0;
}

9. 结构体

编写一个C程序,定义一个结构体Person,包含姓名和年龄两个字段,并创建一个Person类型的变量,初始化其字段,然后打印出该变量的信息。

#include <stdio.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person person;
    strcpy(person.name, "Alice");
    person.age = 30;
    printf("Name: %s, Age: %d\n", person.name, person.age);
    return 0;
}

10. 文件操作

编写一个C程序,创建一个名为“example.txt”的文件,并写入“Hello, World!”。

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Error opening file\n");
        return 1;
    }
    fprintf(file, "Hello, World!");
    fclose(file);
    return 0;
}

11. 错误处理

编写一个C程序,尝试打开一个不存在的文件,并处理错误。

#include <stdio.h>

int main() {
    FILE *file = fopen("nonexistent.txt", "r");
    if (file == NULL) {
        printf("Error opening file\n");
        return 1;
    }
    fclose(file);
    return 0;
}

12. 动态内存分配

编写一个C程序,使用mallocfree函数动态分配和释放内存。

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

int main() {
    int *numbers = (int *)malloc(5 * sizeof(int));
    if (numbers == NULL) {
        printf("Error allocating memory\n");
        return 1;
    }
    for (int i = 0; i < 5; i++) {
        numbers[i] = i;
    }
    free(numbers);
    return 0;
}

13. 指针和数组

编写一个C程序,使用指针访问和修改数组中的元素。

#include <stdio.h>

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int *ptr = numbers;
    for (int i = 0; i < 5; i++) {
        printf("Value at index %d: %d\n", i, *(ptr + i));
        *(ptr + i) *= 2;
    }
    return 0;
}

14. 函数指针

编写一个C程序,定义一个函数指针,并将其指向一个函数,然后调用该函数。

#include <stdio.h>

void printHello() {
    printf("Hello!\n");
}

int main() {
    void (*funcPtr)(void) = printHello;
    funcPtr();
    return 0;
}

15. 结构体指针

编写一个C程序,定义一个结构体指针,并使用它访问和修改结构体中的字段。

#include <stdio.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person person;
    strcpy(person.name, "Bob");
    person.age = 25;
    Person *ptr = &person;
    printf("Name: %s, Age: %d\n", (*ptr).name, (*ptr).age);
    printf("Name: %s, Age: %d\n", ptr->name, ptr->age);
    return 0;
}

16. 链表

编写一个C程序,定义一个单向链表节点结构体,并创建一个包含3个节点的链表,然后打印出链表中的每个节点。

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

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

Node *createNode(int data) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void printList(Node *head) {
    Node *current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    Node *head = createNode(1);
    Node *second = createNode(2);
    Node *third = createNode(3);
    head->next = second;
    second->next = third;
    printList(head);
    return 0;
}

17. 栈

编写一个C程序,定义一个栈结构体,并实现栈的基本操作(入栈、出栈、判空)。

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

#define MAX_SIZE 10

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

void initStack(Stack *s) {
    s->top = -1;
}

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

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

int pop(Stack *s) {
    if (!isEmpty(s)) {
        return s->data[s->top--];
    } else {
        printf("Stack underflow\n");
        return -1;
    }
}

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

18. 队列

编写一个C程序,定义一个队列结构体,并实现队列的基本操作(入队、出队、判空)。

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

#define MAX_SIZE 10

typedef struct {
    int data[MAX_SIZE];
    int front;
    int rear;
} Queue;

void initQueue(Queue *q) {
    q->front = q->rear = -1;
}

int isEmpty(Queue *q) {
    return q->front == -1;
}

void enqueue(Queue *q, int data) {
    if ((q->rear + 1) % MAX_SIZE == q->front) {
        printf("Queue overflow\n");
    } else {
        if (isEmpty(q)) {
            q->front = q->rear = 0;
        } else {
            q->rear = (q->rear + 1) % MAX_SIZE;
        }
        q->data[q->rear] = data;
    }
}

int dequeue(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue underflow\n");
        return -1;
    } else {
        int data = q->data[q->front];
        if (q->front == q->rear) {
            q->front = q->rear = -1;
        } else {
            q->front = (q->front + 1) % MAX_SIZE;
        }
        return data;
    }
}

int main() {
    Queue queue;
    initQueue(&queue);
    enqueue(&queue, 10);
    enqueue(&queue, 20);
    enqueue(&queue, 30);
    printf("Queue: ");
    while (!isEmpty(&queue)) {
        printf("%d ", dequeue(&queue));
    }
    printf("\n");
    return 0;
}

19. 递归

编写一个C程序,使用递归函数计算阶乘。

#include <stdio.h>

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

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

20. 深度优先搜索

编写一个C程序,使用深度优先搜索算法遍历一个图。

#include <stdio.h>

#define MAX_VERTICES 5

int visited[MAX_VERTICES];

void dfs(int graph[MAX_VERTICES][MAX_VERTICES], int vertex) {
    visited[vertex] = 1;
    printf("%d ", vertex);
    for (int i = 0; i < MAX_VERTICES; i++) {
        if (graph[vertex][i] && !visited[i]) {
            dfs(graph, i);
        }
    }
}

int main() {
    int graph[MAX_VERTICES][MAX_VERTICES] = {
        {0, 1, 0, 0, 0},
        {1, 0, 1, 1, 0},
        {0, 1, 0, 0, 1},
        {0, 1, 0, 0, 0},
        {0, 0, 1, 0, 0}
    };
    int startVertex = 0;
    visited[startVertex] = 1;
    printf("DFS: ");
    dfs(graph, startVertex);
    printf("\n");
    return 0;
}

二、数据结构题(21-40题)

21. 链表反转

编写一个C程序,实现一个单向链表的反转。

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

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

Node *reverseList(Node *head) {
    Node *prev = NULL;
    Node *current = head;
    Node *next = NULL;
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    return prev;
}

int main() {
    Node *head = createNode(1);
    Node *second = createNode(2);
    Node *third = createNode(3);
    head->next = second;
    second->next = third;
    printf("Original list: ");
    printList(head);
    head = reverseList(head);
    printf("Reversed list: ");
    printList(head);
    return 0;
}

22. 栈和队列

编写一个C程序,实现一个栈和队列,并使用它们完成以下任务:

  • 将一个字符串入栈和出栈。
  • 将一个字符串入队和出队。
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 10

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

void initStack(Stack *s) {
    s->top = -1;
}

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

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

int pop(Stack *s) {
    if (!isEmpty(s)) {
        return s->data[s->top--];
    } else {
        printf("Stack underflow\n");
        return -1;
    }
}

typedef struct {
    int data[MAX_SIZE];
    int front;
    int rear;
} Queue;

void initQueue(Queue *q) {
    q->front = q->rear = -1;
}

int isEmpty(Queue *q) {
    return q->front == -1;
}

void enqueue(Queue *q, int data) {
    if ((q->rear + 1) % MAX_SIZE == q->front) {
        printf("Queue overflow\n");
    } else {
        if (isEmpty(q)) {
            q->front = q->rear = 0;
        } else {
            q->rear = (q->rear + 1) % MAX_SIZE;
        }
        q->data[q->rear] = data;
    }
}

int dequeue(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue underflow\n");
        return -1;
    } else {
        int data = q->data[q->front];
        if (q->front == q->rear) {
            q->front = q->rear = -1;
        } else {
            q->front = (q->front + 1) % MAX_SIZE;
        }
        return data;
    }
}

int main() {
    char str[] = "Hello, World!";
    Stack stack;
    initStack(&stack);
    for (int i = 0; str[i] != '\0'; i++) {
        push(&stack, str[i]);
    }
    printf("Stack: ");
    while (!isEmpty(&stack)) {
        printf("%c ", pop(&stack));
    }
    printf("\n");

    Queue queue;
    initQueue(&queue);
    for (int i = 0; str[i] != '\0'; i++) {
        enqueue(&queue, str[i]);
    }
    printf("Queue: ");
    while (!isEmpty(&queue)) {
        printf("%c ", dequeue(&queue));
    }
    printf("\n");
    return 0;
}

23. 链表查找

编写一个C程序,实现一个单向链表的查找功能,查找值为target的节点。

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

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

Node *createNode(int data) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void appendNode(Node **head, int data) {
    Node *newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
    } else {
        Node *current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

Node *findNode(Node *head, int target) {
    Node *current = head;
    while (current != NULL) {
        if (current->data == target) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

int main() {
    Node *head = NULL;
    appendNode(&head, 1);
    appendNode(&head, 2);
    appendNode(&head, 3);
    appendNode(&head, 4);
    appendNode(&head, 5);

    int target = 3;
    Node *foundNode = findNode(head, target);
    if (foundNode != NULL) {
        printf("Node with value %d found\n", foundNode->data);
    } else {
        printf("Node with value %d not found\n", target);
    }
    return 0;
}

24. 链表排序

编写一个C程序,实现一个单向链表的排序功能,使用插入排序算法。

”`c #include