Home > front end >  Why doesn't the ' ' sign work with printf for unsigned values?
Why doesn't the ' ' sign work with printf for unsigned values?

Time:09-25

I just wrote and ran following program. This just gave an unexpected output without sign printed to U...MAX.

#include <limists.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    // ...
    printf("LLONG_MIN:  % lli\n", LLONG_MIN);
    printf("LLONG_MAX:  % lld\n", LLONG_MAX);
    printf("ULLONG_MAX: % llu\n", ULLONG_MAX);
    return EXIT_SUCCESS;
}

And I got this:

CHAR_BIT:   8
SCHAR_MIN:  -128
SCHAR_MAX:   127
UCHAR_MAX:  255
SHRT_MIN:   -32768
SHRT_MAX:    32767
USHRT_MAX:  65535
INT_MIN:    -2147483648
INT_MAX:     2147483647
UINT_MAX:   4294967295
LONG_MIN:   -2147483648
LONG_MAX:    2147483647
ULONG_MAX:  4294967295
LLONG_MIN:  -9223372036854775808
LLONG_MAX:   9223372036854775807
ULLONG_MAX: 18446744073709551615

Why is there no sign printed for U..._MAX? How can I do that?

CodePudding user response:

This feature is rather poorly documented by the standard, we can only read this (7.21.6.1):

The result of a signed conversion always begins with a plus or minus sign. (It begins with a sign only when a negative value is converted if this flag is not specified.)

My take is that this should be read as in must always be followed by a signed number conversion specifier, such as %d or %f. You used an unsigned specifier %u so my take is that your code has undefined behavior since it mixes with %u.

CodePudding user response:

Why there is no sign printed for U..._MAX? How can I do that?

If it is unsigned you format it like this, moving the plus sign in front of the format specifier:

printf("...:  %...\n", ...);

If it is signed you format it like you currently do:

printf("...: % ...\n", ...);

I'd guess the reason printf doesn't sign it, is because it's unsigned. Many of the format specifiers are platform or library dependent, so you may encounter a library that adds the plus sign for an unsigned, when it sees % .

CodePudding user response:

The conversion specifier u is not a signed conversion specifier while the flag is used with signed conversion specifiers.

From the C Standard (7.21.6.1 The fprintf function)

  • The result of a signed conversion always begins with a plus or minus sign. (It begins with a sign only when a negative value is converted if this flag is not specified.)

So you have gotten an expected result. The flag with an unsigned conversion specifier has no effect.

The title of your question would sound more correctly if the words unsigned values were substituted for the words unsigned conversion specifiers. That is there is no great sense to prefix the sign or - to values that are always outputted as non-negative. When there are outputted signed values then it makes sense to output them with an explicit sign to make the output more readable to distinguish positive and negative values for example when they are outputted in a table

  • Related