Solution for Exercise 21-10
ex2110
#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, %d/%d/%d\n", *(weekday+today->tm_wday), today->tm_mon+1, today->tm_mday, today->tm_year+1900); return(0); }
Notes
* The 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 displayed by using pointer notation at Line 21. It's the *(p+x)
variation, which is similar to array[x]
. In fact, here is the equivalent way of expressing that line by using an array:
* I hope you remembered to modify Lines 22 and 24 so that the month and year are displayed properly.
* Sample output:
Copyright © 1997-2024 by QPBC.
All rights reserved