1. Defining variables
Fundamental objective in all computer programs is to manipulate some data and from that data calculate some results. Essential element in this process is a piece of memory that you can call your own, you can name it and you can store your data. Each individual piece of memory specified in that way is called variable.
Each variable will store particular kind of data, and the type of data that can be stored is fixed when you define the variable in your program. One variable might store whole numbers or integers, in which case, you couldn’t store numbers with fractional values. The value of that each variable contains at any point is determined by the statements in your program, and of course its value will usually change many times as the program calculation progresses.
Now we will cover and explain the procedure for naming these variables.
1.1 Naming the variables
The name that you give to the variable is called the identifier or a variable name. Variable names can include the letters from A to z, digits 0 to 9, and underscore character. This is a big pallet of letters, numbers and underscores character. No other characters are allowed because your program when you try to compile it will not work. You will get the error message when you try to compile your program code. Variable name must begin with letter or an underscore.
The name of the variable can be up to 2048 characters long. This generally depends on which software you use for programming but the number of 2048 characters suggests you that you have reasonable amount of flexibility in what you call your variables. In fact, as well as variables, there are quite a few other things that have names in C++, and they, too, can have names of up to 2048 charactersm with the same definition rules as a variable name. Using names of the maximum length allowed can make youre programs a little difficult to read, and unless you have amazing keyboard skills, they are the very devil to type in. A more serious consideration is that not all compilers support such long names. If you anticipate compiling your code in other environments, it’s a good idea to limit names to a maximum of 31 characters; that will usually be adequate for devising meaningful names and will avoid problems of compiler name length constraints in most instances.
You can use variable names that begin with an underscore; for example _this1 or _that2. Another thing you must be careful not to name your variable as a function or an system variable because you will produce an error. You should also avoid using names starting with a double underscore for the same reasons.
For example let’s say that you want to name variable Ancient_Aliens. This name variable is ok but don’t forget to use the underscore character. When you give a name Ancient Aliens without underscore the program would not execute because there is a blank space between these two words. Whitespace characters in general cannot appear within a name, and if you inadvertently include whitespace characters, you will have two or more names instead of one, and this will give a reason to compiler to complain. Also note that the variable names such as Angel and angel are quite different, as names they are case-sensitive, so upper- and lowercase letters are differentiated.
1.2. Keywords in C++
In software that you use for programming such as Visual Basic there are reserved words called KEYWORDS that have significance within the language. They will be highlighted with a particular color by the Visual C++ editor as you enter your program. The usual color is blue. If you type a keyword and this keyword does not appear highlighted, then you have entered the keyword incorrectly. The keywords are case – sensitive. The program that you entered earlier in the chapter contained keywords int and return. If you instead of that write Int or Return, these are not the keywords and therefore will not be recognized as such.
1.3. Declaring the variables
A variable declaration is a program statement that specifies the name of a variable of a given type. For example:
this will declare a variable with the name value that can store integers. The type of data that can be stored in the variable value is specified by the keyword int, so you can only use value to store data of type int. Because int is a keyword, you can’t use int as a name for one of your variables.
A single declaration can specify the names of several variables, but as I have said, it is generally better to declare variables in individual statements, one per line. I ’ ll deviate from this from time to time in this book, but only in the interests of not spreading code over too many pages. In order to store data (for example, the value of an integer), you not only need to have defined the name of the variable, you also need to have associated a piece of the computer’s memory with the variable name. This process is called variable definition. In C++, a variable declaration is also a definition (except in a few special cases, which we shall come across during the book). In the course of a single statement, we introduce the variable name, and also tie it to an appropriately sized piece of memory.
So, the statement
is both a declaration and a definition. You use the variable name value that you have declared to access the piece of the computer ’ s memory that you have defined, and that can store a single value of type int.
1.4. Initial Values for Variables
When you declare a variable, you can also assign an initial value to it. A variable declaration that assigns an initial value to a variable is called an initialization . To initialize a variable when you declare it, you just need to write an equals sign followed by the initializing value after the variable name. You can write the following statements to give each of the variables an initial value:
int value = 0;
int count = 10;
int number = 5;
In this case, value will have the value 0, count will have the value 10 , and number will have the value 5 . There is another way of writing the initial value for a variable in C++ called functional notation . Instead of an equals sign and the value, you can simply write the value in parentheses following the variable name. So, you could rewrite the previous declarations as:
Generally, it ’ s a good idea to use either one notation or the other consistently when you are initializing variables. However, I ’ ll use one notation in some examples and the other notation in others, so you get used to seeing both of them in working code. If you don ’ t supply an initial value for a variable, then it will usually contain whatever garbage was left in the memory location it occupies by the previous program you ran (there is an exception to this that you will meet later in this chapter). Wherever possible, you should initialize your variables when you declare them. If your variables start out with known values, it makes it easier to work out what is happening when things go wrong. And one thing you can be sure of — things will go wrong.