
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Correct Way to Use printf to Print 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 may not always produce an error. The correct way to print size_t variables is use of "%zu", because compiler standard %zu is defined as a format specifier for this unsigned type.
Following is the description of "%zu" format:
- z is a length modifier.
- u stands for an unsigned type.
C Example to Print Value of size_t Variable
In this example, we demonstrate a C program that correctly prints a size_t variable:
#include <stdio.h> #include <stddef.h> // for size_t int main() { size_t count = 100; printf("The value of count is %zu\n", count); return 0; }
The above code produces the following result:
The value of count is 100
C++ Example to Print Value of size_t Variable
The following example demonstrates how to print a variable of type size_t in C++:
#include <iostream> #include <cstdio> // for printf #include <cstddef> // for size_t int main() { size_t count = 100; printf("The value of count is %zu\n", count); return 0; }
The above code produces the following result:
The value of count is 100
In conclusion, the program was very simple that uses printf (from the C language) to print a number stored in a size_t variable. Even though its combine C and C++ features to print the value of size_t.