|
Variables |
A variable is a place to store a piece of information. Just as you might store a friend's phone number in your own memory, you can store this information in a computer's memory. Variables are your way of accessing your computer's memory.
Of course, your memory changes over time. Your friend moves across country and has a new phone number, and your friend's new phone number will replace the old one in your memory. Over time, as you get new friends, your memory will keep changing to store different pieces of information. Likewise, a computer's memory can change over time, if you tell it to. Since variables are your access point to your computer's memory, it makes sense that you'd be able to change the information in a computer's memory; otherwise, they wouldn't be called variables (they'd be called statics ).
Why should you care about variables? Variables are the essence of any computer program! Without variables, computers would be useless. Imagine a program which asks the user for two numbers and adds them together, and prints the result.
# AddTwoNumbers Enter the first number: 2 Enter the second number: 5 The sum of 2 and 5 is 7. #
Sounds simple, right? But let's do a little role-playing to see what the computer has to do to execute this program. Instead of this interaction between a person and a computer, let's imagine the same kind of conversation between two people, Ahmed and Jassim.
Ahmed: Salam Alykoum Jassim, I just learned how to add two numbers together.After Jassim says "2", Ahmed has to store that number in his memory somewhere. It may be stored in short-term memory, but he has to store it somewhere before Jassim gives him the second number. Even if Jassim were to give him two numbers in the same sentence, sohe would have to store the numbers somewhere in his memory to add them together.
Jassim: Really!
Ahmed: Give me the first number.
Jassim: 2.
Ahmed: Ok, and give me the second number.
Jassim: 5.
Ahmed: Ok, here's the answer: 2 + 5 = 7.
Jassim: Ohhh! This guy is unbelievable!
In the sample program described above, the computer would most likely store "2" in a variable, then store "5" in a variable, and then calculate the sum by calculating the sum of the numbers store in the two variables.
Although there are similarities between a person's memory and a computer's memory, there are some pretty big differences. In C++, you need to grab a little piece of the computer's memory before you can use it. In other words, you have to tell the computer that you're planning to store a number in a variable before you can actually do it. This is called declaring a variable. To declare a variable, you need to know what kind of information it will store (i.e., will it store a number, or a text-string, or something else) and how you plan to refer to the variable (i.e., the variable's name). C++ imposes fairly strict rules on how you can name your variables.
|
Variable Names
|
Identifiers ( variables names) : are composed of any combination of letters ,digits and underscores (_) selected according to the following rules :
The first character of the name must be a letter or underscore .Cannot start with a digit .
Only letters ,digits or underscores may follow the initial letter . Blank spaces and special characters are not allowed .
Identifiers cannot be one of the keywords .
Identifiers have a maximum of 31 character .
int,
for,
else, and
class.
You can, however, use keywords in the middle of a variable name, such as
"foreign" or "classical".
Example of Invalid identifiers :
1ab ( begin with a number)
e*4 (contains a special character)
while ( is a keyword )