Variables |
|
A computer is equipped with a memory area known as Random Access Memory or RAM that we will illustrate as a group of small boxes: When a computer comes up (boots), part of this memory is filled with some values that primary applications, such as the operating system, need. Since the other parts of the memory are not used, they may be filled with garbage, that is, insignificant values you can't use because they mean nothing: The memory (RAM) is huge enough to contain various values. The particularity of this memory is that it is temporary. In other words, it holds its values for a while and looses them when they are not needed anymore (an example is when the computer shuts down, the RAM is emptied). Normally, the operating system is in charge of the management of this whole memory, including when to put ("push") things in it and when to remove ("pop") things from it. This memory is also made available to you so that, while your application is "running", you can "put" things in it. To better manage its memory, the computer sets aside an amount of memory space for programs like yours so that, when your application runs, it can use that memory to put values in it. This area of memory is called the stack. As you may guess from its name, values are entered ("pushed") in it from a side, like entering a tunnel. Another particularity of this scenario is that the values are removed ("popped") from another side: Keep in mind that the operating system (OS) removes any value when it (the OS) judges or finds out that the value(s) is(are) not used or needed anymore. When your application runs, it will use (part of) this memory space as necessary. One way you can use it is to put values in the space as you judge necessary.
In order to store these values in the memory, you must tell the computer how much space each value will need. This is because the compiler cannot predict what your program will need. When specifying the amount of space, the compiler will reserve an amount of space for each value you specify, in the stack memory. To locate that memory, you must give it a name. The name is not stored as a value; it only allows you to be able to refer to the space memory that was reserved for your variable. Reserving an area of memory is referred to as declaring a variable. There are rules you must follow when doing this. The general formula used to declare a variable in C is: TypeOfVariable VariableName; When you ask the compiler to reserve an amount of memory space for your variable, you must tell it how much memory the variable will need. This is because different values are meant to do different things. For example, while a number can be used to hold the salary of an employee, a word or a group of words is necessary to hold the name of an employee. The second piece of information you must specify, as mentioned already, is a name that you will use to refer to the memory that was reserved for a particular variable. If you want to declare more than one variable of the same type, you can declare each with the type, its name, and its semi-colon. The formula to follow would be: TypeOfVariable Variable1; TypeOfVariable Variable2; TypeOfVariable Variable_n; An alternative is to use one data type followed by the names of variables that would share this type. The names of variables must be separated by commas. The formula to follow would be: TypeOfVariable Variable1, Variable2, Variable_n; If you want to declare different variables that use different data types, you can declare each with the type, its name, and its semi-colon, on the same line. The formula to follow would be: TypeOfVariable1 Variable1; TypeOfVariable2 Variable2; TypeOfVariable3 Variable3; A better alternative is to declare each variable on its own line. This makes the program easier to read. The formula to follow would be: TypeOfVariable1 Variable1; TypeOfVariable2 Variable2; TypeOfVariable3 Variable3;
A name can consist of one word such as country. A name could also be a combination of more than one word, such as firstname or dateofbirth. The C++ and the C++/CLI languages have a list of words reserved for its own use and you must not use any of these words to name your objects or functions. The reserved words are:
Avoid starting the name of a variable with two underscores; sometimes, the compiler would think that you are trying to use one of the words reserved for the compiler. C++ is case-sensitive; this means that CASE, Case, case, and Case are four completely different words. To make your programming experience easier and personal, you can add your own rules to those above. Some (most) companies also adopt a naming convention throughout their documentation. Throughout this book, a name will:
|
|
||
Home | Copyright © 2006-2007 FunctionX, Inc. | Next |
|