返回

初学C语言必背必会的C语言100个核心代码精选大全

开发工具

C语言作为一门经典的编程语言,由于其简洁、高效、功能强大的特点,至今依然是很多初学者入门编程的首选。对于初学者来说,掌握C语言的基础语法和一些常用的代码是非常重要的。本文精心挑选了100个必背必会的C语言代码,涵盖了从基本数据类型到数组、指针、函数等各种C语言知识点。通过学习这些代码,初学者可以快速入门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;
}

二、运算符

#include <stdio.h>

int main() {
    int a = 10;
    int 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;
}

三、控制语句

#include <stdio.h>

int main() {
    int a = 10;

    if (a > 0) {
        printf("a is positive.\n");
    } else {
        printf("a is non-positive.\n");
    }

    return 0;
}

四、函数

#include <stdio.h>

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

int main() {
    int a = 10;
    int b = 3;

    printf("sum(a, b) = %d\n", sum(a, b));

    return 0;
}

五、数组

#include <stdio.h>

int main() {
    int a[10];

    for (int i = 0; i < 10; i++) {
        a[i] = i;
    }

    for (int i = 0; i < 10; i++) {
        printf("a[%d] = %d\n", i, a[i]);
    }

    return 0;
}

六、指针

#include <stdio.h>

int main() {
    int a = 10;
    int *p = &a;

    printf("a = %d\n", a);
    printf("*p = %d\n", *p);

    return 0;
}

七、结构体

#include <stdio.h>

struct student {
    char name[20];
    int age;
};

int main() {
    struct student s = {"John", 20};

    printf("Name: %s\n", s.name);
    printf("Age: %d\n", s.age);

    return 0;
}

八、链表

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

struct node {
    int data;
    struct node *next;
};

struct node *create_node(int data) {
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

int main() {
    struct node *head = create_node(10);
    struct node *second = create_node(20);
    struct node *third = create_node(30);

    head->next = second;
    second->next = third;

    struct node *current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }

    return 0;
}

九、树

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

struct node {
    int data;
    struct node *left;
    struct node *right;
};

struct node *create_node(int data) {
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->left = NULL;
    new_node->right = NULL;
    return new_node;
}

void insert(struct node **root, int data) {
    if (*root == NULL) {
        *root = create_node(data);
    } else if (data < (*root)->data) {
        insert(&(*root)->left, data);
    } else {
        insert(&(*root)->right, data);
    }
}

void inorder_traversal(struct node *root) {
    if (root == NULL) {
        return;
    }

    inorder_traversal(root->left);
    printf("%d ", root->data);
    inorder_traversal(root->right);
}

int main() {
    struct node *root = NULL;

    insert(&root, 10);
    insert(&root, 5);
    insert(&root, 15);
    insert(&root, 2);
    insert(&root, 7);
    insert(&root, 12);
    insert(&root, 20);

    inorder_traversal(root);

    return 0;
}

十、图

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

struct node {
    int data;
    struct node **neighbors;
    int num_neighbors;
};

struct node *create_node(int data) {
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->neighbors = NULL;
    new_node->num_neighbors = 0;
    return new_node;
}

void add_edge(struct node **graph, int num_nodes, int from, int to) {
    struct node *from_node = graph[from];
    struct node *to_node = graph[to];

    from_node->neighbors = (struct node **)realloc(from_node->neighbors, sizeof(struct node *) * (from_node->num_neighbors + 1));
    from_node->neighbors[from_node->num_neighbors++] = to_node;

    to_node->neighbors = (struct node **)realloc(to_node->neighbors, sizeof(struct node *) * (to_node->num_neighbors + 1));
    to_node->neighbors[to_node->num_neighbors++] = from_node;
}

void print_graph(struct node **graph, int num_nodes) {
    for (int i = 0; i < num_nodes; i++) {
        struct node *current = graph[i];
        printf("Node %d: ", current->data);
        for (int j = 0; j < current->num_neighbors; j++) {
            printf("%d ", current->neighbors[j]->data);
        }
        printf("\n");
    }
}

int main() {
    int num_nodes = 5;
    struct node **graph = (struct node ** )malloc(sizeof(struct node *) * num_nodes);

    for (int i = 0; i < num_nodes; i++) {
        graph[i] = create_node(i);
    }

    add_edge(graph, num_nodes, 0, 1);
    add_edge(graph, num_nodes, 0, 2);