Solution for Exercise 21-9

ex2109

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

int main()
{
    time_t tictoc;
    struct tm *today;
    char *weekday[] = {
        "Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"
    };

    time(&tictoc);
    today = localtime(&tictoc);
    printf("Today is %s\n",weekday[today->tm_wday]);
    return(0);
}

Output

Today is Monday

Notes

* An array of weekday string pointers is defined at Line 8. The order of the days is important! The tm_wday element starts numbering days with 0 for Sunday.

* The day of the week is output 15. The weekday member of the tm structure is used as the element number in the weekday array.