gcc 컴파일 중에 format 관련된 경고가 있다.

size_t x = <something>;
printf("size = %u\n", x);

warning: format '%u' expects type 'unsigned int', 
    but argument 2 has type 'long unsigned int'

format specifier 에 적은 형식과 파람의 형식이 다른 경우 경고를 보여준다.

size_t 같은 경우 unsigned int32_t or int64_t 라고 생각해서 u, llu 를 사용했는데 별도의 표시자가 있었다.

size_t x = ...;
ssize_t y = ...;

printf("%zu", x); // unsigned decimal
printf("%zx", x); // hex
printf("%zd", y); // signed decimal

z prefix 가 size_t 에 대한 표시자(length modifier)다. size_t 는 u 를 사용하고 ssize_t 는 d 를 사용한다.(conversion specifier)

참고 : https://en.cppreference.com/w/cpp/io/c/fprintf

 

std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com

int printf( const char* format, ... ); (1) int fprintf( std::FILE* stream, const char* format, ... ); (2) int sprintf( char* buffer, const char* format, ... ); (3) int snprintf( char* buffer, std::size_t buf_size, const char* format, ... ); (4) (since C++1

en.cppreference.com

https://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family

 

How can one print a size_t variable portably using the printf family?

I have a variable of type size_t, and I want to print it using printf(). What format specifier do I use to print it portably? In 32-bit machine, %u seems right. I compiled with g++ -g -W -Wall -W...

stackoverflow.com

 

728x90

+ Recent posts