Home > Backend Development > C++ > body text

Why Does Printing a `uint64_t` Result in a 'Spurious Trailing '%' in Format' Error?

Patricia Arquette
Release: 2024-11-17 21:21:02
Original
815 people have browsed it

Why Does Printing a `uint64_t` Result in a

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;
}
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template