Sams Teach Yourself C++ in 21 Days

(singke) #1
That said, you’re probably working on a computer with a two-byte shortand a four-byte
int, with a four-byte long.
The size of an integer is determined by the processor (16 bit, 32 bit, or 64 bit) and the
compiler you use. On a 32-bit computer with an Intel Pentium processor, using modern
compilers, integers are fourbytes.

44 Day 3


When creating programs, you should never assume the amount of memory
that is being used for any particular type.

CAUTION

Compile and run Listing 3.1 and it will tell you the exact size of each of these types on
your computer.

LISTING3.1 Determining the Size of Variable Types on Your Computer
1: #include <iostream>
2:
3: int main()
4: {
5: using std::cout;
6:
7: cout << “The size of an int is:\t\t”
8: << sizeof(int) << “ bytes.\n”;
9: cout << “The size of a short int is:\t”
10: << sizeof(short) << “ bytes.\n”;
11: cout << “The size of a long int is:\t”
12: << sizeof(long) << “ bytes.\n”;
13: cout << “The size of a char is:\t\t”
14: << sizeof(char) << “ bytes.\n”;
15: cout << “The size of a float is:\t\t”
16: << sizeof(float) << “ bytes.\n”;
17: cout << “The size of a double is:\t”
18: << sizeof(double) << “ bytes.\n”;
19: cout << “The size of a bool is:\t”
20: << sizeof(bool) << “ bytes.\n”;
21:
22: return 0;
23: }

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.

OUTPUT

Free download pdf