简体   繁体   中英

Compiler warning about printf() long unsigned int and uint32_t

In my C code, I'm fprintf ing a "%lu" and giving a uint32_t for the corresponding field. But, when I compile with -Wall in GCC (ver. 4.2.4), I get the following warning:

writeresults.c:16: warning: format '%4lu' expects type 'long unsigned int', but argument 2 has type 
`uint32_t'

Aren't a uint32_t and long unsigned int the same thing on 32-bit architectures? Can this warning be avoided without eliminating the -Wall compiler switch or using a typecast (and if so, how)?

Yes, I'm still using a 32-bit computer/arch/OS/compiler (too poor at the moment to afford new 64-bit HW). Thanks!

uint32_t on x86 Linux with GCC is just unsigned int . So use fprintf(stream, "%4u", ...) (unsigned int) or better yet, fprintf(stream, "%4" PRIu32, ...) (the inttypes.h printf-string specifier for uint32_t ).

The latter will definitely eliminate the compiler warning / error, and, additionally, is cross-platform.

The easiest way to reliably suppress the warning is with a cast:

printf( "%lu", ( unsigned long )x );

"long int" and "int" are different types in C++. You might be looking for the "u" format, which stands for "unsigned int". Of course, this depends on what "uint32_t" is a typedef for on your compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM