16

What am I doing wrong here?

$ cat size.c
#include<stdio.h>
#include<math.h>

int main() {

printf ("sizeof unsigned int = %d bytes.\n", sizeof(unsigned int));
printf ("sizeof unsigned long long = %d bytes.\n", sizeof(unsigned long long));

printf ("max unsigned int = %d\n", (int)(pow(2, 32) - 1));
printf ("max unsigned long long = %lld\n", (unsigned long long)(pow(2, 64) - 1));

}
$ gcc size.c -o size
$ ./size
sizeof unsigned int = 4 bytes.
sizeof unsigned long long = 8 bytes.
max unsigned int = 2147483647
max unsigned long long = -1
$ 

I am expecting 18446744073709551615 as output instead of a -1 at the last line.


Okay, I completely missed that I was getting the wrong value for 232 - 1, which should have been 4294967295, not 2147483647. Things make more sense now.

Lazer
  • 90,700
  • 113
  • 281
  • 364

5 Answers5

29

Just don't suppose that it has a certain value use ULLONG_MAX

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 15
    `#include ` to get this macro, along with others. like `LLONG_MAX` and `LLONG_MIN`. – wkl Oct 09 '10 at 20:24
19

Use %llu, not %lld. d is for signed integers, so printf displays it as a signed long long.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
3

Edit: changed ~0 to (type) -1 as per Christoph's suggestion. See the comments below.

You can get the maximum value of an unsigned type doing the following:

unsigned long long x = (unsigned long long) -1;

Easier, right? =). Second, you are telling printf() to interpret the given variable as a long long decimal, which is signed. Try this instead:

unsigned long long x = (unsigned long long) -1;
printf("%llu", x);

%llu means "long long unsigned".

salezica
  • 74,081
  • 25
  • 105
  • 166
  • 2
    Actually that should be `~0LLu` – Šimon Tóth Oct 09 '10 at 20:27
  • Actually gcc figures that out all by itself. – salezica Oct 09 '10 at 20:28
  • Though I'd expect it to give some sort of warning. – Nathan Fellman Oct 09 '10 at 20:38
  • 2
    @Santiago: gcc doesn't figure anythng out here - without the `llu` suffix, it'll only work as intended if `~0 == -1`; casting `-1` actually is the preferred (and portable) method to get the maximum value of any unsigned type, ie `x = (unsigned long long)-1`; the explicit cast is unnecessary if your warning levels are low enough – Christoph Oct 09 '10 at 20:44
  • Right you are. Edited! Thanks. – salezica Oct 09 '10 at 20:50
  • 1
    Casting is ugly. Simply write `-1ULL`. – R.. GitHub STOP HELPING ICE Oct 09 '10 at 22:29
  • 1
    The casting isn't necessary, using `-1` is completely valid. The standard states that the `int` will first be extended to `long long` then cast to `long long unsigned` (which isn't standardized, but always happily converts). This is the same as putting `(long long unsigned) -1` in the first place. – Matt Joiner Oct 10 '10 at 04:37
  • @Matt: that's why I said that the explicit cast is unnecessary; it may be needed to make your compiler happy, though (gcc may warn about implicit conversions from signed to unsigned) – Christoph Oct 10 '10 at 07:13
  • @R..: what about `size_t` and the types from `stdint.h`, ie if you dont know to which standard integer type your unsigned type corresponds? plain `-1` works fine in all cases, so I prefer that... – Christoph Oct 10 '10 at 07:17
  • @Matt: right, but the reason is even simpler than you state. Conversion from signed types to unsigned types is always just computation modulo: `Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.` – Jens Gustedt Oct 10 '10 at 07:18
2
unsigned long long ulTestMax = -1;
printf ("max unsigned long long = %llu\n", ulTestMax );

this works in C++, should work here, too.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
1

Whoever done -1 to Kiril Kirov post pls take a look here:

Is it safe to use -1 to set all bits to true? Dingo post

In Kiril post only slight modification required regarding sign extension:

unsigned long long ulTestMax = -1LLu; 

-1 is antipattern, it'll do the job if u dont want to go with the solution provided by lmits.h

Community
  • 1
  • 1
cyber_raj
  • 1,780
  • 1
  • 14
  • 25