Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C enum: unknown type name

Tags:

c

enums

I have this simple code:

#include <stdio.h>
#include <time.h>

int main(){
    enum Days { asd=0,Lun,Mar,Mer,Gio,Ven,Sab,Dom };
    Days TheDay;
    time_t ltime;
    struct tm *Tm;

    ltime=time(NULL);
    Tm=localtime(&ltime);
    int j = Tm->tm_wday;
    TheDay = Days(j);

    printf("[%d] %d/%d/%d, %d:%d:%d\n",
            TheDay, /* Mon - Sun */
            Tm->tm_mday,
            Tm->tm_mon,
            Tm->tm_year+1900,
            Tm->tm_hour,
            Tm->tm_min,
            Tm->tm_sec);
}

I don't understand why I'm getting this error:

try.c:6:5: error: unknown type name ‘Days’

like image 316
polslinux Avatar asked Jul 11 '12 15:07

polslinux


1 Answers

This is not C:

enum Days { asd=0,Lun,Mar,Mer,Gio,Ven,Sab,Dom };
Days TheDay;

The name of the new type is enum Days and not Days (which is the enum tag).

You have to use:

enum Days { asd=0,Lun,Mar,Mer,Gio,Ven,Sab,Dom };
enum Days TheDay;

or use a typedef:

typedef enum Days Days;

to be able to use Days as a type name.

like image 136
ouah Avatar answered Sep 28 '22 01:09

ouah