Home > Backend Development > C++ > body text

Why Does My Code Fail to Print a uint64_t with 'spurious trailing '%' in format'?

Barbara Streisand
Release: 2024-11-17 12:17:02
Original
604 people have browsed it

Why Does My Code Fail to Print a uint64_t with

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

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

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!

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