-4

When trying to print 'UINT_MAX' all I get it -1, why is this? It's the only thing I have in my 'main()', a printf statement that prints 'UINT_MAX'

Jordan Baron
  • 165
  • 1
  • 15
  • 2
    Please show your code (preferably a [MCVE](http://stackoverflow.com/help/mcve)). – M.M Dec 07 '16 at 01:38

2 Answers2

2

You used the %d format code, which interprets its argument as a signed int. You're on a two's complement system, so UINT_MAX (0xFFFFFFFF), interpreted as a signed int, is equal to -1. If you want to print it interpreted as unsigned, use %u.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • It's not "interpreted as signed int". It's just undefined behavior. Your format specifiers must match the type you're trying to print. – R.. GitHub STOP HELPING ICE Dec 07 '16 at 02:58
  • 1
    @R.. "format specifiers must match the _promoted_ type" and using `"%d"` or `"%u"` for either `int` or `unsigned` is a specified exception if the value is representable in both types. else its UB to use mis-matched specifier/type. – chux - Reinstate Monica Dec 07 '16 at 03:02
  • @chux: `printf` actually lacks any such exception for when the value is representable in both types. You're thinking of `va_arg`, perhaps. – R.. GitHub STOP HELPING ICE Dec 07 '16 at 03:56
  • @R.. I'm thinking of C11 §6.5.2.2 6 "... , the behavior is undefined, except for the following cases: — one promoted type is a signed integer type, the other promoted type is the corresponding unsigned integer type, and the value is representable in both types; ...". allow using `"%d"` or `"%u"` for either `int` or `unsigned`. – chux - Reinstate Monica Dec 07 '16 at 04:26
  • @chux: That text is about missing prototypes and has nothing to do with `printf`. – R.. GitHub STOP HELPING ICE Dec 07 '16 at 04:59
  • @R.. The larger section specifies "... If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) " which is certainly `printf()`. Yet I doubt we we agree on this matter here - the section is fairly open to interpretation given its complexity.. Perhaps a question for SO, if it has not all ready been made. – chux - Reinstate Monica Dec 07 '16 at 05:10
  • @chux: It has. http://stackoverflow.com/questions/4664100/does-printfx-1-invoke-undefined-behavior Note that I seem to have mistakenly quoted the same passage you just did, which does not specify exceptions for the variadic case, just the no-prototype case. The variadic case is governed by 7.16.1.1 The va_arg macro, but `printf` is not defined in terms of `va_arg`. – R.. GitHub STOP HELPING ICE Dec 07 '16 at 05:15
  • @R.. the prototype of printf has "...", its doesn't simple means that it's use va_arg ? – Stargateur Dec 07 '16 at 06:09
  • @Stargateur: No; formally, it's simply part of the language. – R.. GitHub STOP HELPING ICE Dec 07 '16 at 13:55
0

You can print it with printf()

printf("%u\n", UINT_MAX);

You need to use u specifier, man printf.

Why you get -1 with d specifier? It's because of how work a signed integer in memory.

unsigned integer start at 0, in memory it a simple binary system.

decimal => binary

  • 1 => 1
  • 2 => 10
  • 8 => 1000
  • 255 => 11111111

signed integer use a bit to be interpreted as a negative number, it's confusing because the max value of an unsigned int when it is interpreted like an int is -1.

example with 8 bit integer:

  • -1 => 11111111
  • -128 => 10000000
  • -9 => 11110111

Like you see, the first bit is used to be the sign bit.

Try with this site

Stargateur
  • 24,473
  • 8
  • 65
  • 91