-2

When I compile this code, it gives me

"[Error] unknown type name 'days' ". What am I doing wrong?

#include <stdio.h>

int main(void){

int k=0;
enum days {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};

days dayVariable;

printf("Enter number of the day: ");
scanf("%d",&k);

dayVariable=days(k);

printf("%s", dayVariable);

return 0;

 }
asheeshr
  • 4,088
  • 6
  • 31
  • 50
Lyrk
  • 1,936
  • 4
  • 26
  • 48

4 Answers4

4

If it's C, you must write:

enum days dayVariable;

...

dayVariable = (enum days)k;

If you want days to be the type name, typedef it:

typedef enum {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday} days;

Other than that, you use the wrong format specifier in printf.

P.S.

And remember, C and C++ are different languages. It's obvious from your code and misconceptions that you come from some C++ background. Try to pick up a book on C programming.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

enum is not string. Acutally Monday=0, Tuesday=1, etc. If you want to print string, you may do this:

char *days[] = {"Monday", "Tuesday", ... "Sunday"};

printf("%s", days[k]);
TieDad
  • 9,143
  • 5
  • 32
  • 58
1

In C, enums aren't in the same namespace as normal types. If you want to define a variable typed as that enum, you can do:

enum days dayVariable;

Alternatively you can define a standard type for your enum. There are many similar ways to do that; here is one:

typedef enum {
    /* ... */
} days;
Jan Krüger
  • 17,870
  • 3
  • 59
  • 51
1

This won't compile either:

dayVariable=days(k);

days is not a function, and in C in general type names (which you seem to expect days to be) are not functions either. You might mean:

dayVariable = (enum days) k;

In other words, using a simple cast to the proper type name.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • (enum days) k; seems strange. Do we cast an integer to days? Can you explain this a bit more? – Lyrk Mar 21 '13 at 14:56