结构体创建的 3 种方式
标准方式
#include <stdio.h>
struct student //结构体类型的说明与定义分开。声明
{
int age; /*年龄*/
float score; /*分数*/
char sex; /*性别*/
};
int main ()
{
struct student a={ 20,79,'f'}; //定义
printf("年龄:%d 分数:%.2f 性别:%c\n", a.age, a.score, a.sex );
return 0;
不环保方式
#include <stdio.h>
struct student /*声明时直接定义*/
{
int age; /*年龄*/
float score; /*分数*/
char sex; /*性别*/
/*这种方式不环保,只能用一次*/
} a={21,80,'n'};
int main ()
{
printf("年龄:%d 分数:%.2f 性别:%c\n", a.age, a.score, a.sex );
一次性方式
#include <stdio.h>
struct{
int age;
float score;
char sex;
} t={21,79,'f'};
int main(){
printf("年龄:%d 分数:%f 性别:%c\n", t.age, t.score, t.sex);
return 0;
}
License:
CC BY 4.0