|

When
you define a variable in C++, you must tell the
compiler what kind of variable it is: an integer,
a character, and so forth. This information tells
the compiler how much room to set aside and what
kind of value you want to store in your variable.
Each
cubbyhole is one byte large. If the type of variable
you create is two bytes in size, it needs two bytes
of memory, or two cubbyholes. The type of the variable
(for example, integer) tells the compiler how much
memory (how many cubbyholes) to set aside for the
variable.
Because
computers use bits and bytes to represent values,
and because memory is measured in bytes, it is important
that you understand and are comfortable with these
concepts. For a full review of this topic, please
read Appendix B, "C++ Keywords."
Size
of Integers
On
any one computer, each variable type takes up a
single, unchanging amount of room. That is, an integer
might be two bytes on one machine, and four on another,
but on either computer it is always the same, day
in and day out.
A
char variable (used to hold characters) is most
often one byte long. A short integer is two bytes
on most computers, a long integer is usually four
bytes, and an integer (without the keyword short
or long) can be two or four bytes. Listing 3.1 should
help you determine the exact size of these types
on your computer.
New Term: A character is a single
letter, number, or symbol that takes up one byte
of memory.
Listing
3.1. Determining 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 float is:\t\t" <<
sizeof(float) << " bytes.\n";
10:
cout << "The size of a double is:\t" <<
sizeof(double) << " bytes.\n";
11:
12:
return 0;
13:
}
OUTPUT:
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 float is: 4 bytes.
The
size of a double is: 8 bytes.
NOTE: On your computer, the number
of bytes presented might be different.
Analysis: Most of Listing 3.1 should
be pretty familiar. The one new feature is the use
of the sizeof() function in lines 5 through 10.
sizeof() is provided by your compiler, and it tells
you the size of the object you pass in as a parameter.
For example, on line 5 the keyword int is passed
into sizeof(). Using sizeof(), I was able to determine
that on my computer an int is equal to a short int,
which is 2 bytes.
signed
and unsigned
In
addition, all integer types come in two varieties:
signed and unsigned. The idea here is that sometimes
you need negative numbers, and sometimes you don’t.
Integers (short and long) without the word "unsigned"
are assumed to be signed. Signed integers are either
negative or positive. Unsigned integers are always
positive.
Because
you have the same number of bytes for both signed
and unsigned integers, the largest number you can
store in an unsigned integer is twice as big as
the largest positive number you can store in a signed
integer. An unsigned short integer can handle numbers
from 0 to 65,535. Half the numbers represented by
a signed short are negative, thus a signed short
can only represent numbers from -32,768 to 32,767.
If this is confusing, be sure to read Appendix A,
"Operator Precedence."
Fundamental
Variable Types
Several
other variable types are built into C++. They can
be conveniently divided into integer variables (the
type discussed so far), floating-point variables,
and character variables.
Floating-point
variables have values that can be expressed as fractions—that
is, they are real numbers. Character variables hold
a single byte and are used for holding the 256 characters
and symbols of the ASCII and extended ASCII character
sets.
New Term: The ASCII character set
is the set of characters standardized for use on
computers. ASCII is an acronym for American Standard
Code for Information Interchange. Nearly every computer
operating system supports ASCII, though many support
other international character sets as well.
The
types of variables used in C++ programs are described
in Table 3.1. This table shows the variable type,
how much room this book assumes it takes in memory,
and what kinds of values can be stored in these
variables. The values that can be stored are determined
by the size of the variable types, so check your
output from Listing 3.1.
Table 3.1. Variable Types.
|
Type
|
Size
Values
|
|
unsigned
short
|
int
2 bytes 0 to 65,535
|
|
short
int
|
2
bytes -32,768 to 32,767
|
|
unsigned
long int
|
4
bytes 0 to 4,294,967,295
|
|
long
int
|
4
bytes -2,147,483,648 to 2,147,483,647
|
|
int
(16 bit)
|
2
bytes -32,768 to 32,767
|
|
int
(32 bit)
|
4
bytes -2,147,483,648 to 2,147,483,647
|
|
unsigned
int (16 bit)
|
2
bytes 0 to 65,535
|
|
unsigned
int (32 bit)
|
2
bytes 0 to 4,294,967,295
|
|
char
|
1
byte 256 character values
|
|
float
|
4
bytes 1.2e-38 to 3.4e38
|
|
double
|
8
bytes 2.2e-308 to 1.8e308
|
NOTE:
The sizes of variables might be different from those
shown in Table 3.1, depending on the compiler and
the computer you are using. If your computer had
the same output as was presented in Listing 3.1,
Table 3.1 should apply to your compiler. If your
output from Listing 3.1 was different, you should
consult your compiler’s manual for the values that
your variable types can hold.
|