The sizeof Operator and the climits Header File
The sizeof operator reports that int is 4 bytes on the base system, which uses an 8-bit byte. You can apply the sizeof operator to a type name or to a variable name. When you use the sizeof operator with a type name, such as int, you enclose the name in parentheses. But when you use the operator with the name of the variable, such as n_short, parentheses are optional:
cout << "int is " << sizeof (int) << " bytes.\n";
cout << "short is " << sizeof n_short << " bytes.\n";
The climits header file defines symbolic constants (see the sidebar, “Symbolic Constants the Preprocessor Way,” later in this chapter) to represent type limits. As mentioned previously, INT_MAX represents the largest value type int can hold; this turned out to be 2,147,483,647 for our Windows 7 system. The compiler manufacturer provides a climits file that reflects the values appropriate to that compiler. For example, the climits file for some older systems that used a 16-bit int, defines INT_MAX to represent 32,767. Table 3.1 summarizes the symbolic constants defined in the climits file; some pertain to types you have not yet learned.
Table 3.1. Symbolic Constants from climits
Symbolic Constants the Preprocessor Way
The climits file contains lines similar to the following:
#define INT_MAX 32767
Recall that the C++ compilation process first passes the source code through a preprocessor. Here #define, like #include, is a preprocessor directive. What this particular directive tells the preprocessor is this: Look through the program for instances of INT_MAX and replace each occurrence with 32767. So the #define directive works like a global search-and-replace command in a text editor or word processor. The altered program is compiled after these replacements occur. The preprocessor looks for independent tokens (separate words) and skips embedded words. That is, the preprocessor doesn’t replace PINT_MAXIM with P32767IM. You can use #define to define your own symbolic constants, too (see Listing 3.2). However, the #define directive is a C relic. C++ has a better way of creating symbolic constants (using the const keyword, discussed in a later section), so you won’t be using #define much. But some header files, particularly those designed to be used with both C and C++, do use it.
Initialization
int n_int = INT_MAX;
You can also use literal constants, such as 255, to initialize values. You can initialize a variable to another variable, provided that the other variable has been defined first. You can even initialize a variable to an expression, provided that all the values in the expression are known when program execution reaches the declaration:
int uncles = 5; // initialize uncles to 5
int aunts = uncles; // initialize aunts to 5
int chairs = aunts + uncles + 4; // initialize chairs to 14
Moving the uncles declaration to the end of this list of statements would invalidate the other two initializations because then the value of uncles wouldn’t be known at the time the program tries to initialize the other variables.
The initialization syntax shown previously comes from C; C++ has an initialization syntax that is not shared with C:
int owls = 101; // traditional C initialization, sets owls to 101
int wrens(432); // alternative C++ syntax, set wrens to 432
Caution
If you don’t initialize a variable that is defined inside a function, the variable’s value is
If you know what the initial value of a variable should be, initialize it. True, separating the declaring of a variable from assigning it a value can create momentary suspense:
short year; // what could it be?
year = 1492; // oh
But initializing the variable when you declare it protects you from forgetting to assign the value later.
Initialization with C++11
There’s another format for initialization that’s used with arrays and structures but in C++98 can also be used with single-valued variables:
int hamburgers = {24}; // set hamburgers to 24
Using a braced initializer for a single-valued variable hasn’t been particularly common, but the C++11 standard is extending it some ways. First, it can be used with or without the = sign:
int emus{7}; // set emus to 5
int rheas = {12}; // set rheas to 12
Second, the braces can be left empty, in which case the variable is initialized to 0:
int rocs = {}; // set rocs to 0
int psychics{}; // set psychics to 0