以下是一个例子:

#include <stdio.h>

// 定义结构体
struct Student {
    char name[20];
    int age;
    float score;
};

int main() {
    // 定义结构体变量并赋初始值
    struct Student student1 = {"Tom", 18, 95.5};
    
    // 输出结构体成员的值
    printf("Name: %s\n", student1.name);
    printf("Age: %d\n", student1.age);
    printf("Score: %.1f\n", student1.score);
    
    return 0;
}

在上述例子中,我们定义了一个名为Student的结构体,它有三个成员:name(姓名,类型为字符数组)、age(年龄,类型为整数)和score(分数,类型为浮点数)。

main函数中,我们定义了一个名为student1的结构体变量,并使用花括号进行赋初始值。student1的姓名为"Tom",年龄为18,分数为95.5。

然后,我们使用printf函数输出student1的姓名、年龄和分数。

运行程序,输出如下:

Name: Tom
Age: 18
Score: 95.5
``
C语言结构体定义时赋初始值 给个例子

原文地址: https://www.cveoy.top/t/topic/hDpO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录