简体   繁体   中英

“Unknown type name” in Enum C

It says unknown type name 'week'.. Error showing on 3rd line.

Here is my Code:

#include <stdio.h>

enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday };


void lecture_unit(week day)
{
    if (day == friday) printf("COS10008\n");
    if (day == monday) printf("Maths\n");
    if (day == sunday) printf("Holiday\n");
}

int main()
{
    week today;
    today = sunday;
    lecture_unit(today);
    printf("Day %d\n",today);
    return 0;
}

The correct type name should be enum week instead of just week

#include <stdio.h>

enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday };


void lecture_unit(enum week day)
{
    if (day == friday) printf("COS10008\n");
    if (day == monday) printf("Maths\n");
    if (day == sunday) printf("Holiday\n");
}
int main()
{
    enum week today;
    today = sunday;
    lecture_unit(today);
    printf("Day %d\n",today);
    return 0;
}

If you prefer to use week instead you can use typedef to define type enum week as week

#include <stdio.h>

enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday };
typedef enum week week;

void lecture_unit(week day)
{
    if (day == friday) printf("COS10008\n");
    if (day == monday) printf("Maths\n");
    if (day == sunday) printf("Holiday\n");
}
int main()
{
    week today;
    today = sunday;
    lecture_unit(today);
    printf("Day %d\n",today);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM