Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to use printf to print a size_t?

Tags:

c++

c

Size_t is defined as an unsigned integer, but the size of it depends on whether you're on a 32 or 64-bit machine. What's the correct and portable way to print out a size_t?

like image 986
bradtgmurray Avatar asked Jun 02 '09 15:06

bradtgmurray


People also ask

What format is used to print a string with the printf function?

C++ printf is a formatting function that is used to print a string to stdout. The basic idea to call printf in C++ is to provide a string of characters that need to be printed as it is in the program. The printf in C++ also contains a format specifier that is replaced by the actual value during execution.

What is Size_t type in C?

size_t type is a base unsigned integer type of C/C++ language. It is the type of the result returned by sizeof operator. The type's size is chosen so that it can store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits, on a 64-bit one 64 bits.

How do I print a function in printf?

Generally, printf() function is used to print the text along with the values. If you want to print % as a string or text, you will have to use '%%'. Neither single % will print anything nor it will show any error or warning.


1 Answers

Try using the %zu format string

size_t val = get_the_value(); printf("%zu",val); 

The z portion is a length specifier which says the argument will be size_t in length.

Source - http://en.wikipedia.org/wiki/Printf#printf_format_placeholders

like image 144
JaredPar Avatar answered Oct 12 '22 13:10

JaredPar