cscanf

Format of int32_t in sscanf in C


I need to read (with sscanf) integer number that fits uint32_t. This number is given in hexadecimal base. What format should I use to read and print such number?


Solution

  • You have special macros in the header file inttypes.h

    #include <stdint.h>
    #include <stdio.h>
    #include <inttypes.h>
    
    int main(void)
    {
        uint32_t x;
        if(scanf("%" SCNx32, &x) == 1)
        {
            printf("Scanned successfully. The value is %" PRIx32 "\n", x);
        }
        else
        {
            printf("scanf error\n");
        }
    }