Question about incompatible printf format specifiers

I was just looking at the man page for printf and something happened to me. I was wondering if there are “legal entities of the language” here that could answer a relatively simple question :-P.

Thus, the modifier 't' is defined as

The following integer conversion corresponds to the ptrdiff_t argument.

So what does it mean if you combine this with the conversion of unsigned integers? Clearly, o, u, x, and X should all be interpreted as unsigned values, while d and i are signed.

In addition, for all modifiers (int / unsigned int, size_t/ ssize_tetc.) there are signed / unsigned versions, except ptrdiff_t.

In practice, nothing bad happens, since unsigned versions of types occupy the same space as signed versions. Thus, rights to bytes fly out of the stack.

Thus, nothing "bad" happens, in fact, it prints the expected value for all things tested, except " INT_MIN" (assuming that sizeof(int) == sizeof(ptrdiff_t).

printf("%tu %td\n", INT_MIN, INT_MIN);

prints

2147483648 -2147483648

in a 32-bit system.

Does this standard have any opinion on this? I believe the answer will be "undefined behavior". But I decided that I would ask;).

+5
source share
1 answer

Nothing is visible here. The code you wrote is legal.

Just some facts about why:

  • /
  • ptrdiff_t . . ( size_t - ssize_t C, POSIX)
  • t d, i, o, u, x, x types
+3

All Articles