uint64_t a{ 0 };
printf("%llu", a);

MSVC 에서 uint64_t 형식을 printf 로 출력하려면 %llu 를 사용한다.

ConsoleApplication1.cpp: In function ‘int main()’:
ConsoleApplication1.cpp:10:16: warning: format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘uint64_t’ {aka ‘long unsigned int’} [-Wformat=]
   10 |     printf("%llu", a);
      |             ~~~^   ~
      |                |   |
      |                |   uint64_t {aka long unsigned int}
      |                long long unsigned int
      |             %lu

하지만 g++ 로 컴파일 하면 바로 format 관련 경고가 뜬다. 

typedef signed char        int8_t;
typedef short              int16_t;
typedef int                int32_t;
typedef long long          int64_t;
typedef unsigned char      uint8_t;
typedef unsigned short     uint16_t;
typedef unsigned int       uint32_t;
typedef unsigned long long uint64_t;

MSVC 에서는 uint64_t 를 unsigned long long 으로 선언한 반면 g++ 에서는 long unsigned int 로 취급해서 %lu 를 사용해야한다.

uint64_t a{ 0 };
printf("%" PRIu64, a);

MSVC, g++ 양쪽에서 문제없이 컴파일 되려면 형식 지정자로 cinttypes 헤더에 선언된 PRIu64 와 같은 매크로를 사용하면 된다. 

참고 : https://stackoverflow.com/questions/14535556/why-doesnt-priu64-work-in-this-code

 

Why doesn't PRIu64 work in this code?

As per this answer, I tried printing a uint64_t, but it gives me an error: error: expected ``)' before 'PRIu64' Following is the minimal code showing what I am trying to do: #define

stackoverflow.com

참고 : https://en.cppreference.com/w/cpp/types/integer

 

Fixed width integer types (since C++11) - cppreference.com

int8_tint16_tint32_tint64_t(optional) signed integer type with width of exactly 8, 16, 32 and 64 bits respectivelywith no padding bits and using 2's complement for negative values(provided if and only if the implementation directly supports the type) (type

en.cppreference.com

 

728x90

+ Recent posts