Simple application of enumerated types and unions

Code area

Regarding enumerated types: the
default order starts from 0;
initialization is not allowed, but the following assignments are correct

#include<stdio.h>
main()
{
 enum color{red=3,blue,yellow)a;   //red=3写在外面就是错的,因为red本身就是一个常量
 a=blue;
 printf("%d\n",a);   
}


About the common
output information; assign a value to a member; initialize

#include<stdio.h>
main()
{
 typedef union student
 {
  int a;
  char c;
  float b;
 }s;
 s x;   //定义变量
 x.a=4;
 x.c='a';
 x.b=2.9;
 printf("%d\n",x.a);  //只看最后一个成员,注意输出格式
 union student x={.c='j'};  //对某个成员赋初值
 union student x={16};  //默认对第一个成员初始化
}
Published 57 original articles · praised 54 · visits 2361

Guess you like

Origin blog.csdn.net/September_C/article/details/104931565