Variables

C++ would be of limited use if you could not use it to store the results of calculations, whether in memory or in a disk file. Variables allow you to give names to areas of memory for storing such results. Variables in C++ have definite types, such as int, double, etc., and more complex types that are built from these basic types.

Declarations

A declaration is a statement that sets aside some space in memory. It consists of a type, followed by a list of variables. A variable can contain a value, and you often set this value when you are declaring the variable. For example, the following declaration defines a variable k of type int, and it allocates 4 bytes for it:

;> int k  = 10;
						

After a variable has been defined, you can use it in any expression. For example, you can use the variable k as follows:

;> 2*k - 1;
(int) 19

If you don't declare a variable, it is considered an error. The system could detect such cases and implicitly declare a variable (as occurs in BASIC and many scripting languages), but this is a bad practice in real programs because it is too easy to misspell a name. Likewise, you cannot redeclare a variable, unless the variables are used in different contexts (which we will discuss in Chapter 2 ). UnderC tends to be more easygoing about variable redeclaration in interactive mode.

;> m;
CON 4:parse error
CON 4:Cannot find 'm'

You do not have to initialize the value in a declaration (that is, set the variable to some initial value):

;> int n;
;> n;
(int) n = 0

The value is 0 in this case, but the language does not guarantee that variables will be initially set to zero. You will see in Chapter 2 that in some common situations, declaring variables leaves their value completely undefined.

You can declare a number of variables in one statement. This statement declares six uninitialized variables:

;> int i1, i2, i3, alice, bonzo_the_clown, SinbadTheSailor;
						

There are rules for variable names: They may contain alphabetic characters, digits, and the underscore character (_). They can be lowercase or uppercase, but the system is case sensitive, so two variables a and A are considered distinct from one another.

CAUTION

Case-sensitivity in C++ is different from how most languages (including English!) do it so watch out for this. The name “Sinbad” is not the same as “SINBAD” in C++. The following declaration is completely legal, but it is silly because these are all distinct variables:

;>long onedog=1,Onedog=2,OneDog=3,ONEDOG=4;

There is a problem here because (a) these are different variables and (b) people usually won't read them as different variables. (Most other computer languages would regard these as the same variable; confusion is the result.) The best way to avoid this silliness is to have a naming convention. Most programmers prefer to make all variables all lowercase and use underscores (_) to separate words (for example, one_dog rather than onedog).


C++ supplies the operator sizeof, which you can use to determine how large (in bytes) a variable or type is:


;> float f;
;> double g;
;> sizeof(f);
(int) 4
;> sizeof(g);
(int) 8
;> sizeof(int);
(int) 4

Occasionally you will need to know the size of a type, such as int, in a program. Never assume that it is 4 bytes, and always use sizeof(int) to ensure that the program can still run on machines where sizeof(int) isn't 4. For example, on a SGI workstation an int is 8 bytes.

Assigning Values to Variables

After a variable is declared, you can put any value into it, provided that the value is of a compatible type. This is called assignment:


;> int n;
;> n
							=
							42;
(int) 42
;> n
							=
							2*n;
(int) 84

NOTE

A common problem is that people (quite naturally) confuse the operator = with mathematical equality. Since the first time you could grab a pencil, you have been brought up to read = as meaning “is equal to.” It is indeed true that after the assignment in the preceding example, n is equal to 42. But = didn't mean they were equal before, because the variable's initial value was probably 0. And n = 2*n makes no sense whatsoever as a statement about equality. Other languages avoid this trouble by using an operator that won't be confused with equality (for example, := in Pascal). A good habit to cultivate is to read n = 42 as 'n becomes 42' or 'assign 42 to n,' rather than 'n equals 42'. The operator = does not compare two numbers; rather, it actively takes the right-hand side and puts it into the variable on the left-hand side, which is modified.


The variable can be used in a subsequent expression, and it will have the new value until the next assignment. An interesting fact is that the assignment statement n = 42 is actually an expression: It has a value! This has two important consequences. First, you can say m = n = 0, because n = 0 is an expression that has the value 0, and so m also becomes 0. (You may find it easier to read this statement as m = (n = 0).) Second, you can put assignments where other languages would never allow them. However, this is often a bad idea, and it can be a cause of much confusion.

We discussed earlier in the chapter that a variable has a given precision, which is the number of bytes it uses. It is safe to assign a numerical type that has a smaller precision to a variable. For example, you can assign an integer to a double variable; this process is called promotion. What happens if you try to assign a value that is larger than that variable can hold? This is very easy to show by using unsigned char because char can hold up to 255, and unsigned means the value will be displayed as both a character and as a decimal integer:


;> unsigned char ch;
;> ch = 32;
(unsigned char) ' ' (32)
;> ch = 300;
(unsigned char) ',' (44)

The value 32 is fine; it is the ASCII value of a space. But 300 is too big, and we get 300 – 256, which equals 44, which is a comma character. This is called integer overflow, and it happens with any integer type. Even 32-bit integers overflow eventually; if you keep a Windows 9x machine up for more than 49 days, the 32-bit clock counter (in milliseconds) overflows, and the system becomes unstable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70