Compiler warning about printf () long unsigned int and uint32_t

In my C code, I fprintf ing a "%lu" and gives uint32_t for the corresponding field. But when I compile with -Wall in GCC (version 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' 

Not the same uint32_t and long unsigned int on 32-bit architectures? Can this warning be avoided without exception to the -Wall compiler -Wall or using the typecast method (and if so, how)?

Yes, I still use the 32-bit computer / arch / OS / compiler (too weak at the moment to provide a new 64-bit HW). Thanks!

+4
source share
3 answers

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

The latter will definitely eliminate the compiler warning / error and, in addition, will be cross-platform.

+9
source

The easiest way to reliably suppress a warning is with a throw:

  printf ("% lu", (unsigned long) x);
+4
source

"long int" and "int" are different types in C ++. Perhaps you are looking for the format "u", which means "unsigned int". Of course, this depends on the fact that "uint32_t" is a typedef for your compiler.

+1
source

Source: https://habr.com/ru/post/1339255/


All Articles