What is the correct way to use printf to print a size_t in C/C++?

CC++Server Side Programming

We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”.

In “%zu” format, z is a length modifier and u stand for unsigned type.

The following is an example to print size_t variable.

Example

 Live Demo

#include <stdio.h>
int main() {
   size_t a = 20;
   printf("The value of a : %zu", a);
   return 0;
}

Output

The value of a : 20

In the above program, a variable of size_t length is declared and initialized with a value.

size_t a = 20;

The variables of size_t length is printed as follows −

printf("The value of a : %zu", a);
raja
Published on 08-Nov-2018 10:45:28
Advertisements