Reputation: 117761
I know that UINT32_MAX
exists, but I haven't been able to use it. I tried printf("%d\n", UINT32_MAX);
and it printed out -1
. Using %ld
instead of %d
presented me with the error that UINT32_MAX
is of the type unsigned int and needs %d
to print it out.
Please help, what I ideally want is a macro/enum that holds the maximum value of word_t
which is a type defined by me which currently is uint32_t
.
I hope that I made clear what I want, if not please feel free to ask.
EDIT
I forgot to say what I'm actually trying to do. All of this will be used to set an array of integers all to their maximum value, because that array of integers actually is a bitmap that will set all bits to 1.
Upvotes: 7
Views: 18706
Reputation: 80325
%d
is for signed integers. Use %u
.
EDIT: Ignore this answer and use James's, which is more complete.
Upvotes: 2
Reputation: 106247
You encountered your specific problem because %d
is a signed formatter.
There are a number of ways to fix it (two have already been suggested), but the really correct way is to use the format specifiers defined in <inttypes.h>
:
uint32_t number;
printf("number is %" PRIu32 "\n", number);
Upvotes: 4
Reputation: 355167
The portable way to print a uintN_t
object is to cast it to a uintmax_t
and use the j
length modifier with the u
conversion specifier:
printf("%ju\n", (uintmax_t)(UINT32_MAX));
The j
means that the argument is either an intmax_t
or a uintmax_t
; the u
means it is unsigned, so it is a uintmax_t
.
Or, you can use the format strings defined in <inttypes.h>
(n this case, you'd use PRIu32
):
printf("%" PRIu32 "\n", UINT32_MAX);
You can't just use %u
because it isn't guaranteed that int
is represented by at least 32 bits (it only needs to be represented by at least 16 bits).
Upvotes: 13
Reputation: 1161
If you're setting an array of unsigned int to the max values you could do it via memset:
memset(array, 0xFF, sizeof(unsigned int) * arraysize);
Upvotes: 0