How do I print uint32_t and uint16_t variables value?

后端 未结 2 2105
甜味超标
甜味超标 2020-11-29 01:08

I am trying to print an uint16_t and uint32_t value, but it is not giving desired output.

#include 
#include 

int main()
         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 01:38

    You need to include inttypes.h if you want all those nifty new format specifiers for the intN_t types and their brethren, and that is the correct (ie, portable) way to do it, provided your compiler complies with C99. You shouldn't use the standard ones like %d or %u in case the sizes are different to what you think.

    It includes stdint.h and extends it with quite a few other things, such as the macros that can be used for the printf/scanf family of calls. This is covered in section 7.8 of the ISO C99 standard.

    For example, the following program:

    #include 
    #include 
    int main (void) {
        uint32_t a=1234;
        uint16_t b=5678;
        printf("%" PRIu32 "\n",a);
        printf("%" PRIu16 "\n",b);
        return 0;
    }
    

    outputs:

    1234
    5678
    

提交回复
热议问题