|
|
|
|
|
|
|
int (without the keyword short or long) can be two or four bytes. If you are running Windows 95, Windows 98, or Windows NT, you can count on your int being four bytes as long as you use a modern compiler. |
|
|
|
|
|
|
|
|
Listing 3.1 should help you determine the exact size of these types on your computer using your particular compiler. |
|
|
|
|
|
|
|
|
LISTING 3.1 DETERMINES THE SIZE OF VARIABLE TYPES ON YOUR COMPUTER. |
|
|
|
 |
|
|
|
|
1: #include <iostream.h>
2:
3: int main()
4: {
5: cout << The size of an int is:\t\t << sizeof(int)
<< bytes.\n;
6: cout << The size of a short int is:\t << sizeof(short)
<< bytes.\n;
7: cout << The size of a long int is:\t << sizeof(long)
<< bytes.\n;
8: cout << The size of a char is:\t\t << sizeof(char)
<< bytes.\n;
9: cout << The size of a bool is:\t\t << sizeof(bool)
<< bytes.\n;
10: cout << The size of a float is:\t\t << sizeof(float)
<< bytes.\n;
11: cout << The size of a double is:\t << sizeof(double)
<< bytes.\n;
12:
13: return 0;
14: } |
|
|
|
|
|
|
|
|
The size of an int is 2 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 bool is 1 bytes.
The size of a float is 4 bytes.
The size of a double is 8 bytes. |
|
|
|
|
|
|
|
|
On your computer, the number of bytes presented might be different! |
|
|
|
|
|
|
|
|
|
Analysis: Most of Listing 3.1 should be familiar. The one new feature is the use of the sizeof() function in lines 5 through 10. sizeof() is provided by your compiler, |
|
|
|
|
|