printf Formatting Challenge: Handling uint64_t
In an attempt to print a uint64_t variable, you may encounter an error message similar to: "spurious trailing '%' in format." This issue arises due to the reliance on the ISO C99 standard's format macros, which are not universally defined.
The resolution lies in explicitly requesting the definition of these macros. Here's how to do it:
Setting the __STDC_FORMAT_MACROS Define
Add the following line to the top of your code:
#define __STDC_FORMAT_MACROS
This define instructs the compiler to include the necessary format macros, ensuring that the PRIu64 macro is recognized and available for use.
Once this define is in place, the following code will compile successfully:
#include <inttypes.h> #include <stdio.h> int main() { uint64_t ui64 = 90; printf("test uint64_t : %" PRIu64 "\n", ui64); return 0; }
By specifying the __STDC_FORMAT_MACROS define, you ensure that the uint64_t variable is correctly formatted and printed, resolving the issue you encountered earlier.
The above is the detailed content of Why Does My Code Fail to Print a uint64_t with 'spurious trailing '%' in format'?. For more information, please follow other related articles on the PHP Chinese website!