Every variable in a C++ program must be giEarlier we mentioned thesizeofoperator that is used to determine the number
of bytes a given data type occupies in memory. The following program reports on
the sizes of the various basic data types we have discussed.
The output of this program depends on the computer on which it is run. The
following show the results of running this code on two different machines.
Notice that anintis 4 bytes on both machines and so, in both cases,intvariables
can hold values from−2
31
to 2
31
−1.
Note also thatdoublevariables on both machines are 8 bytes, but this does not
tell us the range of values thatdoublevalues can take.
For thefloatanddoubledata types, there are three quantities of interest: the
largest value, the smallest positive value, and the difference between 1 and the small-
est value greater than 1. This latter value is known as the epsilon value of the data
type. Knowing that a doubleis 8 bytes does not immediately reveal these three
quantities.
C++ provides header files with this information. The header fileclimitsgives
the minimum and maximum value of various integer types. It defines symbols such
asINT_MINandINT_MAXthat are equal to the smallest and largest value an int
may hold. Similarly, the header filecfloatgives the minimum positive, maximum,
and epsilon values forfloat, double, and long double(if available) data types.
Running Codes
#include
using namespace std;
/
**
*
Report on the size of various C++ data types.
*
*
This program may give different results when run on different
*
computers depending on how each of the fundamental data types is
*
defined on those platforms.
*
/
int main() {
// Integer types:
cout << "The size of short is " << sizeof(short)
<< " bytes" << endl;
cout << "The size of int is " << sizeof(int)
<< " bytes" << endl;
cout << "The size of long is " << sizeof(long)
<< " bytes" << endl;
// long long might not exist on all computers
cout << "The size of long long is " << sizeof(long long)
<< " bytes" << endl;
// Character and boolean types:
cout << "The size of char is " << sizeof(char) << " bytes" << endl;
cout << "The size of bool is " << sizeof(bool) << " bytes" << endl;
// Floating point types
cout << "The size of float is " << sizeof(float)
<< " bytes" << endl;
cout << "The size of double is " << sizeof(double)
<< " bytes" << endl;
// long double might not exist on all computers
cout << "The size of long double is " << sizeof(long double)
<< " bytes" << endl;
return 0;
}
The size of short is 2 bytes
The size of int is 4 bytes
The size of long is 4 bytes
The size of long long is 8 bytes
The size of char is 1 bytes
The size of bool is 4 bytes
The size of float is 4 bytes
The size of double is 8 bytes
The size of long double is 8 bytes
The size of short is 2 bytes
The size of int is 4 bytes
The size of long is 4 bytes
The size of long long is 8 bytes
The size of char is 1 bytes
The size of bool is 1 bytes
The size of float is 4 bytes
The size of double is 8 bytes
The size of long double is 12 bytes
No comments:
Post a Comment