Showing the size of data types C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: nil

To do: Showing the size of C++ data types C++ programming

To show: How to view the size of C++ data types in C++ programming

 

// using predefined sizeof() function, to display the data type size, 1 byte = 8 bits

#include <iostream>

 

int main(void)

{

char MyName[20] = "Run Run Show";

 

// fully qualified class name need to be used because we omit the "using namespace std;" directive

std::cout<<"The size of an int is:\t\t"<<sizeof(int)<<" bytes.\n";

std::cout<<"The size of a short int is:\t"<<sizeof(short)<<" bytes.\n";

std::cout<<"The size of a long int is:\t"<<sizeof(long)<<" bytes.\n";

std::cout<<"The size of a char is:\t\t"<<sizeof(char)<<" bytes.\n";

std::cout<<"The size of a float is:\t\t"<<sizeof(float)<<" bytes.\n";

std::cout<<"The size of a double is:\t"<<sizeof(double)<<" bytes.\n";

std::cout<<"The size of a bool is:\t\t"<<sizeof(bool)<<" bytes.\n";

std::cout<<"The size of a MyName is:\t"<<sizeof(MyName)<<" bytes.\n";

 

return 0;

}

 

Output example:

 

The size of an int is: 4 bytes.

The size of a short int is: 2 bytes.

The size of a long int is: 4 bytes.

The size of a char is: 1 bytes.

The size of a float is: 4 bytes.

The size of a double is: 8 bytes.

The size of a bool is: 1 bytes.

The size of a MyName is: 20 bytes.

Press any key to continue . . .

 

 

 

C and C++ Programming Resources | C & C++ Code Example Index