{{format('0')}} {{format('564')}} {{format('1504')}}

【C语言】枚举类型 [ 编程杂谈 ]

大数据男孩 文章 正文

想做一个技术博客,奈何实力不够
分享

明妃

{{nature("2022-08-14 17:23:19")}}更新

枚举类型定义

enum 枚举类型名 { 枚举值名1, 枚举值名2, 枚举值名3,... }; // 默认枚举值从 0 开始

enum Color {blue, green, yellow};

[mark]()

自定义枚举值

默认值 0 开始,我们可以部分自定义,或者全部自定义

枚举值只能是 整形 int

部分自定义

#include <stdio.h>

int main(void) {

    enum A {Aa = 2, Ab, Ac, Ad};
    printf("Aa= %d, Ab= %d, Ac= %d, Ad= %d\n", Aa, Ab, Ac, Ad);

    enum B {Ba, Bb, Bc = 10, Bd};
    printf("Ba= %d, Bb= %d, Bc= %d, Bd= %d\n", Ba, Bb, Bc, Bd);

    return 0;
}

[mark]()

全部自定义

enum A {Aa =2, Ab =3, Ac =4, Ad =5};

枚举类型的使用-工作日&周末判断

#include <stdio.h>
#include <time.h> // 引入时间库

int main(void){

    enum Week {sum, mon, tue, wed, thu, fri, sat};
    enum Week today;
    struct tm *p;  // 时间结构体指针
    time_t t; // 获取时间指针

    time(&t);
    p = localtime(&t);

    today = (int)p->tm_wday; // 今天星期几

    switch(today){
        case mon:
        case tue:
        case wed:
        case thu:
        case fri:
            printf("工作日好好工作吧~~~~");
            break;
        case sat:
        case sum:
            printf("周末了,可以玩耍啦~~~~");
            break;
        default:
            printf("出错了呀~~~~");
    }
    return 0;
}

[mark]()

评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Spark 2.0 单机模式与集群模式 安装

{{nature('2020-01-02 16:47:07')}} {{format('12523')}}人已阅读

TOP 3
Office 2016 Pro Plus 激活

{{nature('2019-12-11 20:43:10')}} {{format('9410')}}人已阅读

TOP 4
Linux上 MySQL 开启远程登陆的两种方法

{{nature('2019-12-26 17:20:52')}} {{format('7405')}}人已阅读

TOP 5
Linux 安装 MySQL 5.7

{{nature('2019-12-26 16:03:55')}} {{format('4882')}}人已阅读

目录

标签云

C语言 枚举

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏