views:

136

answers:

2
+5  Q: 

C format specifier

While i am working ,somewhere inside the code i saw the following staements. I am getting confused by the format specifier in sprintf

   d_number = strtol( tmp_buf , (char **)NULL, 16);
   memset( tmp_buf , ' ' , sizeof( tmp_buf ) );
   sprintf( tmp_buf , "%0.*d" , (int)sizeof( dec_number ) , d_number  );

could anybody explain please?

+6  A: 

.* means the precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. (d_number)

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

stacker
A: 

The * is replaced by (int)sizeof(dec_number). If dec_number is an int, on many machines it is the same as %0.4d. This is the precision with which to print the integer or the maximum number of digits to print.

The * can appear on either side of the ".", for example:

sprintf(tmp_buf, "%*.*", 0, (int)sizeof(dec_number), d_number);

also works.

Richard Pennington