Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unknown type name" in Enum C

Tags:

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;
}
like image 982
Pasan Jayawickrama Avatar asked Sep 15 '19 17:09

Pasan Jayawickrama


1 Answers

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;
}

like image 93
VietHTran Avatar answered Sep 28 '22 15:09

VietHTran