Solution for Exercise 21-6

ex2106

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

int main()
{
    time_t tictoc;
    struct tm *today;

    time(&tictoc);
    today = localtime(&tictoc);
    printf("Today is %d/%d/%d\n",
        today->tm_mon+1,
        today->tm_mday,
        today->tm_year+1900);
    return(0);
}

Output

Today is 5/18/2020

Notes

* The tm structure's tm_mon member starts with 0 for January. So to output the proper "human" value, you must add one, shown in Line 12. Likewise:

* The tm structure's tm_year member uses zero to represent the year 1900. To obtain the current year, you must add 1900 to the value, as is down above in Line 14.