iosobjective-cuint32

How do I set a UInt32 to its maximum value


  1. What is the maximum value for a UInt32?

  2. Is there a way I can use the sizeof operator to get the maximum value (as it is unsigned)? So I don't end up with #defines or magic numbers in my code.


Solution

  • There's a macro UINT32_MAX defined in stdint.h which you can use

    #include <stdint.h>
    
    uint32_t max = UINT32_MAX;
    

    More about the relevant header <stdint.h>:

    http://pubs.opengroup.org/onlinepubs/009695299/basedefs/stdint.h.html

    See also: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf

    Original source of the ISO documents: https://www.iso.org/standard/82075.html