I was learning about time related functions in C from here. They demonstrated strftime() function using following example:
#include <stdio.h>
#include <time.h>
#define LEN 150
int main ()
{
char buf[LEN];
time_t curtime;
struct tm *loc_time;
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
// Displaying date and time in standard format
printf("%s", asctime (loc_time));
strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);
fputs (buf, stdout);
return 0;
}
I have goggled about %m specifier in printf(). It says that %m conversion specifier is not C but is a GNU extension to printf. The ‘%m’ conversion prints the string corresponding to the error code in errno.
I know that %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form.
But what is the purpose of %b and %I in this program? I don't understand what is the use of %b & %I? I have never heard about this. Is %I same as %i?
strftime()
formatting bears no relation to sprintf()
formatting. They both use %
symbols and alphabetics, but that's where the resemblance ends. The whole (and sole) point of using strftime()
is to gain control over the format that a date/time value is printed in, just as printf()
is used to control the format that numeric and string data is printed in. Since they're doing wholly different jobs, it is reasonable to get different results (and to reuse the limited alphabet to mean different things) in the two functions.
In strftime()
:
%A
is the full local name of the day of the week.%b
is the abbreviated month name.%d
is the day of the month.%I
is the hour in 12-hour format.%M
is the minute.%p
is the AM/PM indicator.Of those, %b
, %I
and %M
have no meaning in standard printf()
, whereas:
%A
prints a double
in hexadecimal notation with upper-case letters.%d
prints an int
as a decimal.%p
prints a void *
address.