Start New Discussion within our Software Development Community

please guys look at the code below

#include <stdio.h>

int main(void)
{
   enum days {monday=1, tuesday, wednesday, thursday, friday, saturday, sunday};
   enum days today=monday, tomorow=today+1;

   printf("\n today is the %d day of the week\n", today); 
   printf("tomorow is the %d day of the week\n", tomorow);

   printf("\n today is %s and tomorow will be %s\n", today, tomorow);  //did not work

   return 0;

}

so how do i get the third 'printf' print out the variables today and tomorow as strings?

The short answer is you can't (without doing some more work). See that string you typed, "monday"? That does not exist once the compiler is finished. It's in your source code, typed in with all the other things you typed, but in the finished program there are no strings "monday", "tuesday", etc.

So if you want to see "monday" come out on the screen, you're going to have to provide that string. The variable "today" is not a string; it's an enum, which is basically an int, so you need a way to match up that int with the correct string.

One way to do this is to provide a function yourself to match the enum with a string. Something like:

char *stringFromDays(enum days input)
{
    static const char *strings[] = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};

    return strings[input];
}

and call that function along these lines:

printf("\n today is %s and tomorow will be %s\n", stringFromDays(today), stringFromDays(tomorow));

If you insist on Monday being day 1 rather than day zero, you'll need to insert an empty string as the zeroth string in the array.

This article has been dead for over six months. Start a new discussion instead.