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


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);

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements