|
Declaring Variables |
A declaration tells the compiler the name and type of a variable you'll be using in your program. In its simplest form, a declaration consists of the type, the name of the variable, and a terminating semicolon:
char c;
int i;
float f;
You can also declare several variables of the same type in one declaration,
separating them with commas:
int a, b;
Let's say you want to declare a variable of type
int called
myAge. That is to say, the
variable myAge
will store an integer. In C\C++, this is written:
int myAge;
All this does is tell the computer that you plan to use an integer, and that the integer's name is myAge.
placement of declarations |
The placement of declarations is significant. You can't place them just anywhere (i.e. they cannot be interspersed with the other statements in your program). They must either be placed at the beginning of a function, or at the beginning of a brace-enclosed block of statements (which we'll learn about in the next chapter), or outside of any function. Furthermore, the placement of a declaration, as well as its storage class, controls several things about its visibility and lifetime, as we'll see later.
You may wonder why variables must be declared before use. There are two reasons:
It makes things somewhat easier on the compiler; it knows right away what kind of storage to allocate and what code to produce to store and manipulate each variable; it doesn't have to try to intuit the programmer's intentions.
It forces a bit of useful discipline on the programmer: you cannot introduce variables willy-nilly; you must think about them enough to pick appropriate types for them. (The compiler's error messages to you, telling you that you apparently forgot to declare a variable, are as often helpful as they are a nuisance: they're helpful when they tell you that you misspelled a variable, or forgot to think about exactly how you were going to use it.)
Initial Value |
In some languages, variables are initialized to 0 - that is, a variable's initial value will be 0. This is not true of C++! Sometimes your variables will be initialized to 0, but sometimes they will be initialized with garbage. As you might anticipate, this can cause some nasty bugs. Let's take a look at another sample program.
|
#include <iostream.h> |
You might expect the program to output "My
age is 0". In fact, the output of this program is unreliable. On one system you
may get output of "My age is 11"; another system may output "My age is 0"; yet
another system may output "My age is 3145". That's what it means to have a
variable initialized with garbage.
|
Initializing |
It is always a good idea to
initialize
(إعطاء قيمة مبدئية
للمتغير ) your variables with some value. If you don't know what a
variable's initial value should be, initialize it to 0. Initializing a variable
is easy. Let's fix the above program so that it always outputs "My age is 21".
The first line of the
main
function initializes myAge
by assigning it a value immediately. the
equals sign ("=") is called an operator .
|
#include <iostream.h> |
Most of the time, I recommend writing one declaration per line. For the most part, the compiler doesn't care what order declarations are in. You can order the declarations alphabetically, or in the order that they're used, or to put related declarations next to each other. Collecting all variables of the same type together on one line essentially orders declarations by type, which isn't a very useful order (it's only slightly more useful than random order).
|
These declarations are equal |
||
|
int a=1; int b=2; char c='a'; char d; |
int a=1,b=2; char c='a'; char d; |
int a=1,b=2; char c='a',d; |