Why weren’t new printf () format parameters introduced for the new (with width) as part of C99?

When researching how to do cross-platform printf() format strings in C (that is, given the number of bits, I expect every integer argument to printf() should be) I came across this section of the Wikipedia article on printf() . The article discusses non-standard parameters that can be passed to printf() format strings, for example (which looks like an extension for Microsoft):

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

It is further indicated that:

ISO C99 includes an inttypes.h file header, which includes several macros for use in platform-independent printing encoding.

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

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

My question is: did I miss something? Is this really the standard way to C99? If so, why? (Although I'm not surprised that I never saw code that uses format strings in this way, since it seems so cumbersome ...)

+16
c types c99 printf format-specifiers
Jul 26 '09 at 3:52
source share
5 answers

Justification C implies that <inttypes.h> standardizes existing practice:

<inttypes.h> was obtained from a header with the same name that was found on several existing 64-bit systems.

but the rest of the text does not write about these macros, and I don’t remember that they existed at that time.

Only speculation follows, but it is brought up by the experience of the work of standardization committees.

One of the advantages of C99 macros over standardizing an additional format specifier for printf (note that C99 also added some) is that providing <inttypes.h> and <stdint.h> when you already have an implementation that supports the required functions in a particular implementation method, it simply writes two files with an adequate typedef and macros. This reduces the cost of ensuring compliance with the existing implementation, reduces the risk of breaking existing programs that use existing features of the implementation specifics (the standard method does not interfere) and facilitate the transfer of agreed programs to implementations that do not have these (they can be provided by the program). In addition, if specific implementation paths have already changed at that time, this does not contribute to the implementation of one implementation over another.

+16
Jul 26 '09 at 6:53
source share

That's right, says the C99 standard, which you should use. If you really need portablt code that conforms to the 100% standard corresponding to the letter, you should always print int with "%d" and int32_t with "%"PRId32 .

Most people will not worry, as there are very few cases where failure to do so matters. If you are not porting your code to Win16 or DOS, you can assume that sizeof(int32_t) <= sizeof(int) , so it is harmless to accidentally print a int32_t as int . Similarly, a long long has quite a lot of 64 bits (although this is not guaranteed), so printing int64_t as a long long (for example, using the %llx ) is also safe.

The types int_fast32_t , int_least32_t , etc. almost never used, so you can imagine that their respective format specifiers are used even less often.

+8
Jul 26 '09 at 4:11
source share

You can always drop up and use %jd , which is an intmax_t format intmax_t .

 printf("%jd\n", (intmax_t)(-2)); 



I used intmax_t to show that any intXX_t can be used, but simply casting to long much better for the case of int32_t , then use %ld .

+2
Nov 05 2018-11-11T00:
source share

I can only speculate on why. I like AProgrammer to answer above, but there is one aspect missing: what are you going to add to printf as a format modifier? There are already two different ways to use numbers in a printf format string (width and precision). Adding a third type of number to say how many bits of precision in the argument would be great, but where are you going to use it without confusing people? Unfortunately, one of the drawbacks of C is that printf was not intended to be an extension.

Macros are terrible, but when you have to write code that portes to 32-bit and 64-bit platforms, they are a godsend. Definitely saved my bacon.

I think the answer to your question is why is there either

  • No one could think of a better way to do this, or
  • The standards committee could not agree with what they thought was clearly better.
+1
Jul 26 '09 at 17:26
source share

Another possibility: backward compatibility. If you add additional format specifiers to printf or additional options, it is possible that the specifier in some pre-C99 code will have a format string interpreted differently.

When you change C99, you do not change the functionality of printf .

+1
Apr 6 2018-11-11T00:
source share



All Articles