The Program in C++ to Find Size of Fundamental data types is given below:
#include <iostream>
#include <climits> // for INT_MAX and INT_MIN
int main() {
std::cout << "Size of char : " << sizeof(char) << " bytes" << std::endl;
std::cout << "Size of int : " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of short int : " << sizeof(short int) << " bytes" << std::endl;
std::cout << "Size of long int : " << sizeof(long int) << " bytes" << std::endl;
std::cout << "Size of float : " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double : " << sizeof(double) << " bytes" << std::endl;
std::cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" << std::endl;
return 0;
}
Output:
Size of char : 1 bytes
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
Pro-Tips💡
This code uses the sizeof()
operator to determine the size of the fundamental data types char
, int
, short int
, long int
, float
, double
, and wchar_t
.
The sizeof()
operator returns the size of the data type in bytes.
The cout
function is used to print the size of each data type to the screen, along with the string “bytes” for clarity.
Note that It is also possible to check the range of the fundamental data types with the help of climits
library, for example INT_MAX
,INT_MIN
for int
type.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.