温馨提示×

C语言中怎么显示所有记录

小亿
94
2024-05-30 09:29:12
栏目: 编程语言

在C语言中,要显示所有记录,需要使用循环遍历所有记录并将其打印出来。下面是一个示例代码:

#include <stdio.h>

struct Record {
    int id;
    char name[50];
    int age;
};

int main() {
    struct Record records[3] = {
        {1, "Alice", 25},
        {2, "Bob", 30},
        {3, "Charlie", 35}
    };

    for (int i = 0; i < 3; i++) {
        printf("Record %d:\n", i+1);
        printf("ID: %d\n", records[i].id);
        printf("Name: %s\n", records[i].name);
        printf("Age: %d\n", records[i].age);
    }

    return 0;
}

在上面的示例中,定义了一个结构体Record表示记录,然后创建了一个包含3条记录的数组records。接着使用循环遍历数组中的每一条记录,并将其逐条打印出来。

0