Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why weren't new (bit width specific) printf() format option strings adoped as part of C99?

While researching how to do cross-platform printf() format strings in C (that is, taking into account the number of bits I expect each integer argument to printf() should be) I ran across this section of the Wikipedia article on printf(). The article discusses non-standard options that can be passed to printf() format strings, such as (what seems to be a Microsoft-specific extension):

printf("%I32d\n", my32bitInt);

It goes on to state that:

ISO C99 includes the inttypes.h header file that includes a number of macros for use in platform-independent printf coding.

... and then lists a set of macros that can be found in said header. Looking at the header file, to use them I would have to write:

 printf("%"PRId32"\n", my32bitInt);

My question is: am I missing something? Is this really the standard C99 way to do it? If so, why? (Though I'm not surprised that I have never seen code that uses the format strings this way, since it seems so cumbersome...)

like image 972
mpontillo Avatar asked Jul 26 '09 03:07

mpontillo


People also ask

What will happen if you use wrong formatting characters in printf?

Oh yes - printf depends on the format string to determine the size and type of the variable to fetch next. When the format string is wrong it may try to fetch a variable that isn't even there, with all consequences that may have.

How do you specify printf width?

The field width can also be specified as asterisk (*) in which case an additional argument of type int is accessed to determine the field width. For example, to print an integer x in a field width determined by the value of the int variable w, you would write the D statement: printf("%*d", w, x);


1 Answers

The C Rationale seems to imply that <inttypes.h> is standardizing existing practice:

<inttypes.h> was derived from the header of the same name found on several existing 64-bit systems.

but the remainder of the text doesn't write about those macros, and I don't remember they were existing practice at the time.

What follows is just speculation, but educated by experience of how standardization committees work.

One advantage of the C99 macros over standardizing additional format specifier for printf (note that C99 also did add some) is that providing <inttypes.h> and <stdint.h> when you already have an implementation supporting the required features in an implementation specific way is just writing two files with adequate typedef and macros. That reduces the cost of making existing implementation conformant, reduces the risk of breaking existing programs which made use of the existing implementation specifics features (the standard way doesn't interfere) and facilitate the porting of conformant programs to implementation who don't have these headers (they can be provided by the program). Additionally, if the implementation specific ways already varied at the time, it doesn't favorize one implementation over another.

like image 109
AProgrammer Avatar answered Oct 09 '22 09:10

AProgrammer