C语言结构体定义

作者:哈哈小脸 | 创建时间: 2023-07-29
学习C语言中,总结了 C语言结构体定义的三种方式,不敢独享,在这里分享自己的笔记,希望大家都能进步...
C语言结构体定义

操作方法

1. 最标准的方式: #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; }

2 . 不环保的方式 #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  ); return 0; }

3 最奈何人的方式 #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; }

温馨提示

最好用标准的方式:第一种
点击展开全文

更多推荐