Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler warning about printf() long unsigned int and uint32_t

Tags:

c

linux

gcc

printf

In my C code, I'm fprintfing 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!

like image 903
pr1268 Avatar asked Dec 10 '22 11:12

pr1268


1 Answers

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.

like image 67
Conrad Meyer Avatar answered May 08 '23 07:05

Conrad Meyer