Calculation and use of the maximum value of uint32_t

I know that UINT32_MAX exists, but I could not use it. I tried printf("%d\n", UINT32_MAX); and printed -1 . Using %ld instead of %d showed me an error that UINT32_MAX is of type unsigned int and needs %d to print it.

Please help what I ideally want is a macro / enum that contains the maximum value of word_t , which is the type defined by me, which is currently uint32_t .

I hope that I have made it clear that I want, if not a request, do not hesitate to ask.

EDIT

I forgot to say what I'm actually trying to do. All this will be used to set the array of integers to the maximum value, because this array of integers is actually a bitmap that sets all bits to 1.

+6
c gcc c99
source share
4 answers

A portable way to print an uintN_t object is to pass it to uintmax_t and use a length modifier j using the u conversion specifier:

 printf("%ju\n", (uintmax_t)(UINT32_MAX)); 

j means that the argument is either intmax_t or uintmax_t ; u means it is unsigned, therefore it is uintmax_t .

Or you can use format strings defined in <inttypes.h> (in this case, you would use PRIu32 ):

 printf("%" PRIu32 "\n", UINT32_MAX); 

You cannot just use %u because it is not guaranteed that an int is represented by at least 32 bits (it only needs to be represented at least 16 bits).

+11
source share

You encountered your specific problem because %d is a signed formatter.

There are several ways to fix this (two have already been suggested), but the really correct way is to use format specifiers defined in <inttypes.h> :

 uint32_t number; printf("number is %" PRIu32 "\n", number); 
+3
source share

%d - for signed integers. Use %u .

EDIT: ignore this answer and use James, which is more complete.

+2
source share

If you specify an unsigned array of ints to the maximum values, you can do this via memset:

memset (array, 0xFF, sizeof (unsigned int) * arraysize);

0
source share

All Articles