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...
When you ________ an object into a destination file, it can be updated independently from the object in the source file
A) insert B) embed C) attach D) link
The ________ HTML5 element indicates a portion of a document, like a chapter or specific topic
a. header b. article c. aside d. section