其他分享
首页 > 其他分享> > 结构类型

结构类型

作者:互联网

在线C环境:https://clin.icourse163.org/

 

1. 枚举

2. 结构

3. 联合

 

 

 

枚举

 

1. 常量符号化

#include <stdio.h>

const int red = 0;
const int yellow = 1;
const int green = 2;
// enum COLOR {RED, YELLOW, GREEN};
int main() {
    
    int color = -1;
    char *colorName = NULL;
    
    printf("请输入你喜欢的颜色的代码:");
    scanf("%d",&color);
    
    switch(color){
        case red:
        colorName = "red";
        break;
        case yellow:
        colorName = "yellow";
        break;
        case green:
        colorName = "green";
        break;
        default:
        colorName = "unknow";
        break;
    }
    printf("你喜欢的颜色是%s\n",colorName);
    
    
    
    return 0;
}

会报错

⽤符号⽽不是具体的数字来表⽰程序中的数字

 

#include <stdio.h>

// const int red = 0;
// const int yellow = 1;
// const int green = 2;
enum COLOR {RED, YELLOW, GREEN};
int main() {
    
    int color = -1;
    char *colorName = NULL;
    
    printf("请输入你喜欢的颜色的代码:");
    scanf("%d",&color);
    
    switch(color){
        case RED:
        colorName = "red";
        break;
        case YELLOW:
        colorName = "yellow";
        break;
        case GREEN:
        colorName = "green";
        break;
        default:
        colorName = "unknow";
        break;
    }
    printf("你喜欢的颜色是%s\n",colorName);
    
    
    
    return 0;
}

输入0,1,2来检测输出的结果

⽤枚举⽽不是定义独⽴的const int变量

 

 

 

2.案例

 

标签:const,colorName,int,yellow,break,枚举,类型,结构
来源: https://www.cnblogs.com/hechunfeng/p/15655536.html