C 语言变量初始化

时间:2023-01-13 07:17:44


# include <stdio.h>
# include <string.h>
struct AGE
{
int year;
int month;
int day;
};
struct STUDENT
{
char name[20]; //姓名
int num; //学号
struct AGE birthday; /*用struct AGE结构体类型定义结构体变量birthday, 即生日*/
float score; //分数
};
int main(void)
{
struct STUDENT student1; /*用struct STUDENT结构体类型定义结构体变量student1*/
strcpy(student1.name, "小明"); //不能写成&student1
student1.num = 1207041;
student1.birthday.year = 1989;
student1.birthday.month = 3;
student1.birthday.day = 29;
student1.score = 100;
printf("name : %s\n", student1.name); //不能写成&student1
printf("num : %d\n", student1.num);
printf("birthday : %d-%d-%d\n", student1.birthday.year, student1.birthday.month, student1.birthday.day);
printf("score : %.1f\n", student1.score);
return 0;
}
name : 小明
num : 1207041
birthday : 1989-3-29
score : 100.0