Write a program that uses the sizeof operator to determine the sizes in bytes of the various data types on your computer system. Write the results to the file datasize.txt, so that you may print the results later. The results should be displayed in two-column format with the type name in the left column and the size of the type in right column, as in:
char 1
unsigned char 1
short int 2
unsigned short int 2
int 4
unsigned int 4
long int 4
unsigned long int 4
float 4
double 8
long double 10
```
#include
#include
#include
#include
using namespace std;
int main()
{
// assign stream to file and open file
ofstream outFile( "datasize.txt" );
// terminate program if output file cannot be opened
if ( !outFile )
{
cerr << "Unable to open \"datasize.txt\".\n";
exit( 1 );
} // end if
// write size of char, unsigned char,
// short int, unsigned short int and int to file
outFile << "Data type" << setw( 24 ) << "Size\nchar" << setw( 21 )
<< sizeof( char ) << "\nunsigned char" << setw( 12 )
<< sizeof( unsigned char ) << "\nshort int" << setw( 16 )
<< sizeof( short int ) << "\nunsigned short int" << setw( 7 )
<< sizeof( unsigned short ) << "\nint" << setw( 22 )
<< sizeof( int ) << '\n';
// write size of unsigned int, long int, unsigned long int
// float, double and long double to file
outFile << "unsigned int" << setw( 13 ) << sizeof( unsigned )
<< "\nlong int" << setw( 17 ) << sizeof( long )
<< "\nunsigned long int" << setw( 8 ) << sizeof( unsigned long )
<< "\nfloat" << setw( 20 ) << sizeof( float )
<< "\ndouble" << setw( 19 ) << sizeof( double )
<< "\nlong double" << setw( 14 ) << sizeof( long double ) << endl;
} // end main
```
Data type Size
char 1
unsigned char 1
short int 2
unsigned short int 2
int 4
unsigned int 4
long int 4
unsigned long int 4
float 4
double 8
long double 8
You might also like to view...
A(n) ____ is a drawing of a shot, with arrows to show movements of the characters or camera within the shot.
A. panel B. frame C. storyboard D. segment
Using server behaviors to generate dynamic pages is much more efficient than updating content manually each time the information changes.
Answer the following statement true (T) or false (F)
An individual article entered in a blog is known as a ________
Fill in the blank(s) with correct word
Which of the following devices commonly mount on top of the CPU to cool it?
A) Surge protector B) Capacitor C) Heat sink D) Surge resistor