Spurious Trailing '%' in Format Error When Trying to Printf uint64_t
In an attempt to print a uint64_t variable using printf, you may encounter an error such as "spurious trailing '%’ in format." Let's delve into the issue and explore a solution.
The code snippet provided showcases the attempt to print a uint64_t variable using the %" PRIu64 " placeholder:
#include <inttypes.h> #include <stdio.h> int main() { uint64_t ui64 = 90; printf("test uint64_t : %" PRIu64 "\n", ui64); return 0; }
However, the compilation fails with the error message mentioned earlier. To resolve this issue, you need to ensure that the necessary macro is defined. The ISO C99 standard specifies that the PRIu64 macro is only defined if explicitly requested.
Therefore, add the following line to your code before including
#define __STDC_FORMAT_MACROS
With this line added, the PRIu64 macro will be properly defined, and the error you encountered will be resolved. This macro enables the use of the PRIu64 placeholder in the printf format string, allowing you to successfully print the uint64_t variable.
The above is the detailed content of Why Does Printing a `uint64_t` Result in a 'Spurious Trailing '%' in Format' Error?. For more information, please follow other related articles on the PHP Chinese website!