Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 2: Declaring Variables Constantly

33

Declaring Variable Types

So far this chapter has been trumpeting that variables must be declared and that they must be assigned a type. Fortunately (ta-dah!), C++ provides a num­ ber of different variable types. See Table 2-1 for a list of variables, their advan­ tages, and limitations.

Table 2-1

 

C++ Variables

Variable

Example

Purpose

int

1

A simple counting number, either positive or

 

 

negative.

 

 

 

unsigned int

1U

A counting number that’s only non-negative.

 

 

 

long

10L

A potentially larger version of int. There is

 

 

no difference between long and int with

 

 

Dev-C++ and Microsoft Visual C++.NET.

 

 

 

unsigned long

10UL

A nonnegative long integer.

 

 

 

float

1.0F

A single precision real number. This smaller

 

 

version takes less memory than a double

 

 

but has less accuracy and a smaller range.

double

1.0

A standard floating-point variable.

 

 

 

char

‘c’

A single char variable stores a single alpha­

 

 

betic or digital character. Not suitable for

 

 

arithmetic.

 

 

 

string

“this is

A string of characters forms a sentence or

 

a string”

phrase.

bool

true

The only other value is false. No I mean, it’s

 

 

really false. Logically false. Not “false” as

 

 

in fake or ersatz or . . . never mind.

 

 

 

It may seem odd that the standard floating length variable is called double while the “off size” is float. In days gone by, memory was an expensive asset — you could reap significant space savings by using a float variable. This is no longer the case. That, combined with the fact that modern processors perform double precision calculations at the same speed as float, makes the double the default. Bigger is better, after all.

34

Part I: Introduction to C++ Programming

The following statement declares a variable lVariable as type long and sets it equal to the value 1, while dVariable is a double set to the value 1.0:

//declare a variable and set it to 1 long lVariable;

lVariable = 1;

//declare a variable of type double and set it to 1.0 double dVariable;

dVariable = 1.0;

You can declare a variable and initialize it in the same statement:

int nVariable = 1; // declare a variable and

// initialize it to 1

Although such declarations are common, the only benefit to initializing a vari­ able in the declaration is that it saves typing.

A char variable can hold a single character; a string (which isn’t really a vari­ able but works like one for most purposes) holds a string of characters. Thus, ‘C’ is a char that contains the character C, whereas “C” is a string with one character in it. A rough analogy is that a ‘C’ corresponds to a nail in your hand, whereas “C” corresponds to a nail gun with one nail left in the magazine. (Chap­ ter 9 describes strings in detail.)

If an application requires a string, you’ve gotta provide one, even if the string contains only a single character. Providing nothing but the character just won’t do the job.

Types of constants

A constant is an explicit number or character (such as 1, 0.5, or ‘c’) that doesn’t change. As with variables, every constant has a type. In an expression such as n = 1; (for example), the constant 1 is an int. To make 1 a long integer, write the statement as n = 1L;. The analogy is as follows: 1 represents a single ball in the bed of a pickup truck, whereas 1L is a single ball in the bed of a dump truck. The ball is the same, but the capacity of its container is much larger.

Following the int to long comparison, 1.0 represents the value 1, but in a floating-point container. Notice, however, that the default for floating-point constants is double. Thus, 1.0 is a double number and not a float.

true is a constant of type bool. However, “true” (note the quotation marks) is a string of characters that spell out the word true. In addition, in keeping with C++’s attention to case, true is a constant, but TRUE has no meaning.

Chapter 2: Declaring Variables Constantly

35

Special characters

You can store any printable character you want in a char or string vari­ able. You can also store a set of non-printable characters that is used as character constants. See Table 2-2 for a description of these important nonprintable characters.

Table 2-2

Special Characters

Character Constant

Action

‘\n’

newline

 

 

‘\t’

tab

 

 

‘\0’

null

 

 

‘\\’

backslash

 

 

You have already seen the newline character at the end of strings. This char­ acter breaks a string and puts the parts on separate lines. A newline charac­ ter may appear anywhere within a string. For example,

“This is line 1\nThis is line 2”

appears on the output as

This is line 1

This is line 2

Similarly, the \t tab character moves output to the next tab position. (This position can vary, depending on the type of computer you’re using to run the program.) Because the backslash character is used to signify special charac­ ters, a character pair for the backslash itself is required. The character pair \\ represents the backslash.

C++ collision with file names

Windows uses the backslash character to sep­ arate folder names in the path to a file. (This is a remnant of MS-DOS that Windows has not been able to shake.) Thus, Root\FolderA\ File represents File within FolderA, which is a subdirectory of Root.

Unfortunately, MS-DOS’s use of backslash con­ flicts with the use of backslash to indicate an escape character in C++. The character \\ is a backslash in C++. The MS-DOS path Root\ FolderA\File is represented in C++ string as

Root\\FolderA\\File.