In this tutorial you will learn about the C Program to Print Size of int, float, double and char and its application with practical example.
In this tutorial, we will learn to create a program that finds the size of various data types such as int, float, double and char in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Data Types
- C Variables
- C Input Output
Program to Print Size of int, float, double and char
In this program we will find the size of various data types such as int, float, double and char using sizeof operator. In C Programming, the sizeof operator returns the amount of the memory allocated to a variable of specific datatype such as int, float, double and char. In this program we will first declare and initialize a set variables each one of different data type. Later in the program we will display size of various data types such as int, float, double and char.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int main() { int intVar; float floatVar; double doubleVar; char charVar; // sizeof evaluates the size of a variable printf("Size of int: %zu bytes\n", sizeof(intVar)); printf("Size of float: %zu bytes\n", sizeof(floatVar)); printf("Size of double: %zu bytes\n", sizeof(doubleVar)); printf("Size of char: %zu byte\n", sizeof(charVar)); return 0; } |
Output:-
In the above program, we have first declared and initialized a set variables required in the program.
- intVar = it is a variable of integer data type
- floatVar = it is a variable of float data type
- doubleVar = it is a variable of double data type
- charVar = it is a variable of char data type
Later in the program we will find the size of various data types such as int, float, double and char using sizeof operator and will print using printf statement.