While programming in C++, it is important to know the size of various basic datatypes supported by C++. Below code will give you the size of various basic datatypes.
Code
//============================================================================
// Name : Study.cpp
// Author : Kirteekumar
// Version :
// Copyright :
// Description : size of various types in C++ , Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int main() {
cout << “– size of various types –” << endl;
cout << “size of bool : ” << sizeof(bool) << endl;
cout << “size of char : ” << sizeof(char) << endl;
cout << “size of short : ” << sizeof(short) << endl;
cout << “size of int : ” << sizeof(int) << endl;
cout << “size of long : ” << sizeof(long) << endl;
cout << “size of long long : ” << sizeof(long long) << endl;
cout << “size of float : ” << sizeof(float) << endl;
cout << “size of double : ” << sizeof(double) << endl;
cout << “size of long double : ” << sizeof(long double) << endl;
return 0;
}
Compilation output
13:33:25 **** Incremental Build of configuration Debug for project Study ****
make all
Building file: ../src/Study.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF”src/Study.d” -MT”src/Study.o” -o “src/Study.o” “../src/Study.cpp”
Finished building: ../src/Study.cpp
Building target: Study
Invoking: GCC C++ Linker
g++ -o “Study” ./src/Study.o
Finished building target: Study
13:33:26 Build Finished. 0 errors, 0 warnings. (took 463ms)
Output
— size of various types —
size of bool : 1
size of char : 1
size of short : 2
size of int : 4
size of long : 8
size of long long : 8
size of float : 4
size of double : 8
size of long double : 16