C/C++ Program to Find the Size of int, float, double and char
In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable.
Program to Determine Variable Size
Write a C or C++ program to determine the sizes of the four different types of variables that are given: int, char, float, and double.
Through the sizeof(variable) operation, a variable's size is obtained. Additionally, we can use the %ld format specifier to print the result produced by sizeof.
Examples:
- Input: char
Output: Size of char = 1
- Input: float
Output: Size of float = 4
To determine the Four Variables Sizes:
Following are the four types of variables that are defined:
- integer_Type
- float_Type
- double_Type
- char_Type
With the help of the sizeof() operator, the variables' sizes are determined.
The following is an example of the C and C++ program to evaluate the sizes of the int, char, float, and double types of data:
C Program:
// Using the C programming language, compute the sizes of the int, char, float, and double types of data.
#include <stdio.h>
int main ()
{
int integer_Type;
char char_Type;
float float_Type;
double double_Type;
// Obtain and display the size of the integer type.
printf ( "The size of int type of data is: %ld\n",
sizeof ( integer_Type ) );
// Obtain and display the size of the char type.
printf ( "The size of char type of data is: %ld\n",
sizeof ( char_Type ) );
// Obtain and display the size of the float type.
printf ( "The size of float type of data is: %ld\n",
sizeof ( float_Type ) );
// Obtain and display the size of the double type.
printf ( "The size of double type of data is: %ld\n",
sizeof ( double_Type ) );
return 0;
}
C++ Program:
// Using the C++ programming language, compute the sizes of the int, char, float, and double types of data.
#include <iostream>
using namespace std;
int main ()
{
int integer_Type;
char char_Type;
float float_Type;
double double_Type;
// Obtain and display the size of the integer type.
cout << "The size of int type of data is: " << sizeof ( integer_Type ) << endl;
// Obtain and display the size of the char type.
cout << "The size of char type of data is: " << sizeof ( char_Type ) << endl;
// Obtain and display the size of the float type.
cout << "The size of float type of data is: " << sizeof ( float_Type ) << endl;
// Obtain and display the size of the double type.
cout << "The size of double type of data is: " << sizeof ( double_Type ) ;
return 0;
}
Output:
The size of int type of data is: 4
The size of char type of data is: 1
The size of float type of data is: 4
The size of double type of data is: 8