Solution for Exercise 21-8

ex2108

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

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

    time(&tictoc);
    today = localtime(&tictoc);
    printf("The time is %d:%d:%d\n",
        today->tm_hour,
        today->tm_min,
        today->tm_sec);
    return(0);
}

Notes

* Sample output:

Yes, that's 24-hour format. It's after 10:00 PM, almost my bedtime.

* Other, more troubling sample output:

Above you see output for an early hour of the day. On your own can you fix the program so that the minutes and seconds are output two digits wide padded with a zero if necessary?