C++ Program to Find Size of int, float, double, and char in Your System

by | Jan 4, 2023 | C++, C++ Programs

Introduction

In this program, we shall learn about the size of data types we use in C++ programming. Using the sizeof operator, we will calculate the size of all data types.

This program declares 4 Variables are declared of int, char, double, and float type. particular

The size of the data type is used to allocate space in the memory for that variable.

To understand this program, you should know the following C++ concepts:

1. C data types

2. Variables, literals, and constants in C

3. Sizeof operator

Program

#include<iostream>
using namespace std;
int main()
{
int n;
char ch;
float f;
double d;
cout<<"Size of int: "<<sizeof(n)<<endl;
cout<<"Size of char: "<<sizeof(ch)<<endl;
cout<<"Size of float: "<<sizeof(f)<<endl;
cout<<"Size of double: "<<sizeof(d)<<endl;


return 0;
}

 

Output

Explanation

Four variables, n, ch, f, and d declared of type int, char, float, and double.

cout<<“Size of int: “<<sizeof(n)<<endl;

In the above statement, the sizeof() operator is used to calculate the size of ‘n’. Since ‘n’ is an int type variable, thus the size of the int data type will be printed on the screen.

Similarly, other variables are passed in the same operator to calculate the size of other data types.

Moreover, we can directly pass the data type name in the sizeof operator without using any variable. Then the code become

cout<<“Size of int: “<<sizeof(int)<<endl;

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.