1.1.   Fundamental data types

When you’re writing your program you need to define all the data and all the variables according to ISO/IEC C++ standard. This standard provides you with the range of fundamental data types, which are specified by particular keywords. The word fundamental represents a fundamental data which can be stored in your computer. This fundamental data essentially are numerical values, and they also include characters because a character is represented by a numerical character code. In previous example you saw keyword int, and this keyword is necessary for defining integer variables (numbers).
We can divide fundamental data types into three categories and they are:
·         Integers,
·         Non-integral values – which are called floating-point types,
·         Void type – which specifies an empty set of values or no type

1.1.1.                 Integer variables

Integer variables are variables that only have values that are whole numbers. You know that you can declare integer variables using the keyword int. Variables of type int occupy 4bytes in memory and can store both positive and negative integer values. The upper and lower limits for the values of a variable of type int correspond to the maximum and minimum signed binary numbers, which can be represented by 32 bits. The upper limit for a variable of type int is 2^32-1, which is 2,147,483,647, and the lower limit is – (2^32), which is -2,147,483,648.  Here is an example of defining a variable of type int:
Int a = 25;
In Visual C++, the keyword short also defines an integer variable, but this variable will occupy 2 bytes. The keyword short is the same as short int, so you can use both of them for defining the variable. Here is an example:
Short a = 10;
Short int a = 10;
C++ programming language also provides the integer type long, which can also be written as long int. Here’s how you declare variables of type long:
Long a = 1000000L;
Long b = 0L;
The letter L appended to the end of the literals specifies that they are integers of type long. You can also use the small letter l for the same purpose, but it has the disadvantage that it is easily confused with the digit l. Integer literals without an L appended are of type int.  Integer variables declared as long in Visual C++ occupy 4 bytes and can have values from -2,147,483,648 to 2,147,483,647. This is the same range as for variables declared as type int.
If you need to store integers of an even greater magnitude, you can use variables of type long long. For example:
Long long H = 100000000000L;
Variables of type long long occupy 8 bytes and can store values from  9223372036854775808 to 9223372036854775807. The suffix to identify an integer constant as type long long is LL or ll ,but the latter is best avoided.

1.1.2.                 Character data types

The char data type serves a dual purpose. It specifies a one - byte variable that you can use either to store integers within a given range, or to store the code for a single ASCII character, which is the American Standard C ode for Information Interchange. You can declare a char variable with this statement:
This declares the variable with the name letter and initializes it with the constant ‘ A ’ . Note that you specify a value that is a single character between single quotes, rather than the double quotes used previously for defining a string of characters to be displayed. A string of characters is a series of values of type char that are grouped together into a single entity called an array . I ’ ll discuss arrays and how strings are handled in C++ in Chapter 4.
Because the character ‘A’ is represented in ASCII by the decimal value 35, you could have written the statement as:
char letter = 35; // Equivalent to A
This produces the same result as the previous statement. The range of integers that can be stored in a variable of type char with Visual C++ is from  128 to 127.
NOTE The ISO/IEC C++ standard does not require that type char should represent signed 1 - byte integers. It is the compiler implementer ’ s choice as to whether type char represents signed integers in the range  128 to 127 or unsigned integers in the range 0 to 255. You need to keep this in mind if you are porting your C++ code to a different environment.
The type wchar_t is so called because it is a w ide character type, and variables of this type store 2 - byte character codes with values in the range from 0 to 65,535. Here’s an example of defining a variable of type wchar_t :
wchar_t letter = L'Z'; // A variable storing a 16-bit character code
This defines a variable, letter, that is initialized with the 16 - bit code for the letter Z. The L preceding the character constant, ‘Z’ , tells the compiler that this is a 16 - bit character code value. A wchar_t variable stores Unicode code values. You could have used functional notation here, too:
wchar_t letter(L'Z'); // A variable storing a 16-bit character code
You can also use hexadecimal constants to initialize integer variables, including those of type char, and it is obviously going to be easier to use this notation when character codes are available as hexadecimal values. A hexadecimal number is written using the standard representation for hexadecimal digits: 0 to 9, and A to F (or a to f) for digits with values from 10 to 15. It ’ s also prefixed by 0x (or 0X) to distinguish it from a decimal value. Thus, to get exactly the same result again, you could rewrite the last statement as follows: wchar_t letter(0x5A); // A variable storing a 16-bit character code Notice that Windows XP, Vista, and Windows 7 provide a Character Map utility that enables you to locate characters from any of the fonts available to Windows. It will show the character code in hexadecimal and tell you the keystroke to use for entering the character. You’ll find the Character Map utility if you click on the Start button and look in the System Tools folder that is within the Accessories folder. Integer Type Modifiers Variables of the integral types char , int , short , or long store signed integer values by default, so you can use these types to store either positive or negative values. This is because these types are assumed to have the default type modifier signed. So, wherever you wrote int or long, you could have written signed int or signed long , respectively.
You can also use the signed keyword by itself to specify the type of a variable, in which case, it means signed int . For example:
signed value = -5; // Equivalent to signed int
This usage is not particularly common, and I prefer to use int, which makes it more obvious what is meant. The range of values that can be stored in a variable of type char is from - 128 to 127, which is the same as the range of values you can store in a variable of type signed char . In spite of this, type char and type signed char are different types, so you should not make the mistake of assuming they are the same.
If you are sure that you don’t need to store negative values in a variable (for example, if you were recording the number of miles you drive in a week), then you can specify a variable as unsigned :
unsigned long mileage = 0UL;
Here, the minimum value that can be stored in the variable mileage is zero, and the maximum value is 4,294,967,295 (that’s 2 32  1). Compare this to the range of - 2,147,483,648 to 2,147,483,647 for a signed long. The bit that is used in a signed variable to determine the sign of the value is used in an unsigned variable as part of the numeric value instead. Consequently, an unsigned variable has a larger range of positive values, but it can’t represent a negative value. Note how a U (or u) is appended to unsigned constants. In the preceding example, I also have L appended to indicate that the constant is long. You can use either upper - or lowercase for U and L , and the sequence is unimportant. However, it’s a good idea to adopt a consistent way of specifying such values.
You can also use unsigned by itself as the type specification for a variable, in which case, you are specifying the variable to be of type unsigned int .

1.1.3.                 The Boolean Type

Boolean variables are variables that can have only two values: a value called true and a value called false. The type for a logical variable is bool, named after George Boole, who developed Boolean algebra, and type bool is regarded as an integer type. Boolean variables are also referred to as logical variables. Variables of type bool are used to store the results of tests that can be either true or false, such as whether one value is equal to another.
You could declare the name of a variable of type bool with the statement:
bool testResult;
Of course, you can also initialize variables of type bool when you declare them:
bool colorIsRed = true;
Or like this:
bool colorIsRed(true);
NOTE Remember, both signed and unsigned are keywords, so you can ’ t use them as variable names. NOTE You will fi nd that the values TRUE and FALSE are used quite extensively with variables of numeric type, and particularly of type int . This is a hangover from the time before variables of type bool were implemented in C++, when variables of type int were typically used to represent logical values. In this case, a zero value is treated as false and a non - zero value as true. The symbols TRUE and FALSE are still used within the MFC where they represent a non - zero integer value and 0, respectively. Note that TRUE and FALSE — written with capital letters — are not keywords in C++; they are just symbols defined within the MFC. Note also that TRUE and FALSE are not legal bool values, so don’t confuse true with TRUE .

1.1.4.                 Floating - Point Types

Values that aren’t integral are stored as floating - point numbers. A floating - point number can be expressed as a decimal value such as 112.5, or with an exponent such as 1.125E2 where the decimal part is multiplied by the power of 10 specified after the E (for Exponent). Our example is, therefore, 1.125 × 10 2 , which is 112.5.
NOTE A floating - point constant must contain a decimal point, or an exponent, or both. If you write a numerical value with neither, you have an integer.
You can specify a floating - point variable using the keyword double , as in this statement:
double in_to_mm = 25.4;
A variable of type double occupies 8 bytes of memory and stores values accurate to approximately 15 decimal digits. The range of values stored is much wider than that indicated by the 15 digits accuracy, being from 1.7 × 10  308 to 1.7 × 10 308, positive and negative.
If you don ’ t need 15 digits’ precision, and you don ’ t need the massive range of values provided by double variables, you can opt to use the keyword float to declare floating - point variables occupying 4 bytes. For example:
float pi = 3.14159f;
This statement defines a variable pi with the initial value 3.14159. The f at the end of the constant specifies that it is of type float . Without the f, the constant would have been of type double . Variables that you declare as float have approximately 7 decimal digits of precision and can have values from 3.4 × 10  38 to 3.4 × 10 38, positive and negative.
The ISO/IEC standard for C++ also defines the long double floating - point type, which in Visual C++, is implemented with the same range and precision as type double . With some compilers, long double corresponds to a 16 - byte floating - point value with a much greater range and precision than type double .

1.1.5.                 Fundamental Types in ISO/IEC C++

The following table contains a summary of all the fundamental types in ISO/IEC C++ and the range of values that are supported for these in Visual C++.

true or false
By default, the same as type signed char :   128 to 127. Optionally, you can make char the same range as type
Signed char
-128 to 127
Unsigned char
0 to 255
0 to 65,535
-32,768 to 32,767
Unsigned short
0 to 65,535
-2,147,483,648 to 2,147,483,647
unsigned long  
0 to 4,294,967,295
long long
- 9223372036854775808 to 9223372036854775807
unsigned long long
0 to 18446744073709551615
± 3.4 × 10 ± 38 with approximately 7 digits accuracy
± 1.7 × 10 ± 308 with approximately 15 digits accuracy
Long double
± 1.7 × 10 ± 308 with approximately 15 digits accuracy

1.1.6.                 Literals

I have already used a lot of explicit constants to initialize variables, and in C++, constant values of any kind are referred to as literals. A literal is a value of a specific type, so values such as 23, 3.14159, 9.5f , and true are examples of literals of type int , type double , type float , and type bool , respectively. The literal “Samuel Beckett ” is an example of a literal that is a string, but I’ll defer discussion of exactly what type this is until Chapter 4. Here’s a summary of how you write literals of various types.
char , signed char , or unsigned char
‘ A ’ , ‘ Z ’ , ‘ 8 ’ , ‘ * ’
L ’ A ’ , L ’ Z ’ , L ’ 8 ’ , L ’ * ’
- 77, 65, 12345, 0x9FE
unsigned int
10U, 64000u
- 77L, 65L, 12345l
unsigned long
5UL, 999999UL, 25ul, 35Ul
long long
- 777LL, 66LL, 1234567ll
unsigned long long
55ULL, 999999999ULL, 885ull, 445Ull
3.14f, 34.506F
1.414, 2.71828
long double
1.414L, 2.71828l
true, false

You can ’ t specify a literal to be of type short or unsigned short , but the compiler will accept initial values that are literals of type int for variables of these types, provided the value of the literal is within the range of the variable type.
You will often need to use literals in calculations within a program, for example, conversion values such as 12 for feet into inches or 25.4 for inches to millimeters, or a string to specify an error message. However, you should avoid using numeric literals within programs explicitly where their significance is not obvious. It is not necessarily apparent to everyone that when you use the value 2.54, it is the number of centimeters in an inch. It is better to declare a variable with a fi xed value corresponding to your literal instead — you might name the variable inches To Centimeters , for example. Then, wherever you use inches To Centimeters in your code, it will be quite obvious what it is. You will see how to fi x the value of a variable a little later on in this chapter.

1.1.7.                 Defining Synonyms for Data Types

The typedef keyword enables you to define your own type name for an existing type. Using typedef, you could define the type name Big Ones as equivalent to the standard long int type with the declaration:
typedef long int BigOnes; // Defining BigOnes as a type name
This defi nes BigOnes as an alternative type specifi er for long int , so you could declare a variable mynum as long int with the declaration:
BigOnes mynum = 0L; // Define a long int variable
There ’ s no difference between this declaration and the one using the built - in type name. You could equally well use
long int mynum = 0L; // Define a long int variable
for exactly the same result. In fact, if you define your own type name such as BigOnes , you can use both type specifi ers within the same program for declaring different variables that will end up as having the same type. Because typedef only defines a synonym for an existing type, it may appear to be a bit superficial, but it is not at all. You’ll see later that it fulfills a very useful role in enabling you to simplify more complex declarations by defining a single name that represents a somewhat convoluted type specification. This can make your code much more readable.

1.1.8.                 Variables with Specific Sets of Values

You will sometimes be faced with the need for variables that have a limited set of possible values that can be usefully referred to by labels — the days of the week, for example, or months of the year. There is a specific facility in C++ to handle this situation, called an enumeration . Take one of the examples I have just mentioned — a variable that can assume values corresponding to days of the week. You can define this as follows:
enum Week{Mon, Tues, Wed, Thurs, Fri, Sat, Sun} thisWeek;
This declares an enumeration type with the name Week and the variable thisWeek, which is an instance of the enumeration type Week that can assume only the constant values specified between the braces. If you try to assign to thisWeek anything other than one of the set of values specified, it will cause an error. The symbolic names listed between the braces are known as enumerators. In fact, each of the names of the days will be automatically defined as representing a fixed integer value. The first name in the list, Mon , will have the value 0, Tues will be 1, and so on. You could assign one of the enumeration constants as the value of the variable thisWeek like this:
thisWeek = Thurs;
Note that you do not need to qualify the enumeration constant with the name of the enumeration. The value of thisWeek will be 3 because the symbolic constants that an enumeration defines areassigned values of type int by default in sequence, starting with 0.
By default, each successive enumerator is one larger than the value of the previous one, but if you would prefer the implicit numbering to start at a different value, you can just write:
enum Week {Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} thisWeek;
Now, the enumeration constants will be equivalent to 1 through 7. The enumerators don ’ t even need to have unique values. You could defi ne Mon and Tues as both having the value 1, for example, with the statement:
enum Week {Mon = 1, Tues = 1, Wed, Thurs, Fri, Sat, Sun} thisWeek;
As the type of the variable thisWeek is type int, it will occupy 4 bytes, as will all variables that are of an enumeration type. Note that you are not allowed to use functional notation for initializing enumerators. You must use the assignment operator as in the examples you have seen. Having defi ned the form of an enumeration, you can define another variable as follows:
enum Week nextWeek;
This defi nes a variable nextWeek as an enumeration that can assume the values previously specified. You can also omit the enum keyword in declaring a variable, so, instead of the previous statement, you could write:
Week next_week;
If you wish, you can assign specific values to all the enumerators. For example, you could define this enumeration:
enum Punctuation {Comma = ',', Exclamation = '!', Question = '?'} things;
Here, you have defined the possible values for the variable things as the numerical equivalents of the appropriate symbols. The symbols are 44, 33, and 63, respectively, in decimal. As you can see, the values assigned don’t have to be in ascending order. If you don’t specify all the values explicitly, each enumerator will be assigned a value incrementing by 1 from the last specified value, as in our second Week example. You can omit the enumeration type if you don’t need to define other variables of this type later. For example:
enum {Mon, Tues, Wed, Thurs, Fri, Sat, Sun} thisWeek, nextWeek, lastWeek;
Here, you have three variables declared that can assume values from Mon to Sun . Because the enumeration type is not specified, you cannot refer to it. Note that you cannot define other variables for this enumeration at all, because you would not be permitted to repeat the definition. Doing so would imply that you were redefining values for Mon to Sun, and this isn’t allowed.
Share on Google Plus