Using Variables, Declaring Constants in C++
This chapter explores the need and use of variables in the C++ programming language.
Variables are tools that help the programmer temporarily store data for a finite amount of time. Constants are tools that help the programmer define artifacts that are not allowed to change or make changes.
In this lesson, you find out
How to declare and define variables and constants
How to assign values to variables and manipulate those values
How to write the value of a variable to the screen
How to use keywords auto and constexpr
What Is a Variable?
Before you actually explore the need and use of variables in a programming language, take a step back and first see what a computer contains and how it works.
Memory and Addressing in Brief
All computers, smart phones, and other programmable devices contain a microprocessor and a certain amount of memory for temporary storage called Random Access Memory (RAM). In addition, many devices also allow for data to be persisted on a storage device such as the hard disk. The microprocessor executes your application, and in doing so it works with the RAM to fetch the application binary code to be executed as well as the data associated with it, which includes that displayed on the screen and that entered by the user.
The RAM itself can be considered to be a storage area akin to a row of lockers in the dorms, each locker having a number—that is, an address. To access a location in memory, say location 578, the processor needs to be asked via an instruction to fetch a value from there or write a value to it.
Declaring Variables to Access and Use Memory
The following examples will help you understand what variables are. Assume you are writing a program to multiply two numbers supplied by the user. The user is asked to feed the multiplicand and the multiplier into your program, one after the other, and you need to store each of them so that you can use them later to multiply. Depending on what you want to be doing with the result of the multiplication, you might even want to store it for later use in your program. It would be slow and error-prone if you were to explicitly specify memory addresses (such as 578) to store the numbers, as you would need to worry about inadvertently overwriting existing data at the location or your data being overwritten at a later stage.
When programming in languages like C++, you define variables to store those values. Defining a variable is quite simple and follows this pattern:
VariableType VariableName;
or
VariableType VariableName = InitialValue;
The variable type attribute tells the compiler the nature of data the variable can store, and the compiler reserves the necessary space for it. The name chosen by the programmer is a friendly replacement for the address in the memory where the variable’s value is stored. Unless the initial value is assigned, you cannot be sure of the contents of that memory location, which can be bad for the program. Therefore, initialization is optional, but it’s often a good programming practice. Listing 3.1 shows how variables are declared, initialized, and used in a program that multiplies two numbers supplied by the user.
LISTING 3.1 Using Variables to Store Numbers and the Result of Their Multiplication
1: #include <iostream> 2: using namespace std; 3: 4: int main () 5: { 6: cout << "This program will help you multiply two numbers" << endl; 7: 8: cout << "Enter the first number: "; 9: int firstNumber = 0; 10: cin >> firstNumber; 11: 12: cout << "Enter the second number: "; 13: int secondNumber = 0; 14: cin >> secondNumber; 15: 16: // Multiply two numbers, store result in a variable 17: int multiplicationResult = firstNumber * secondNumber; 18: 19: // Display result 20: cout << firstNumber << " x " << secondNumber; 21: cout << " = " << multiplicationResult << endl; 22: 23: return 0; 24: }
Output
This program will help you multiply two numbers Enter the first number: 51 Enter the second number: 24 51 x 24 = 1224
Analysis
This application asks the user to enter two numbers, which the program multiplies and displays the result. To use numbers entered by the user, it needs to store them in the memory. Variables firstNumber and secondNumber declared in Lines 9 and 13 do the job of temporarily storing integer values entered by the user. You use std::cin in Lines 10 and 14 to accept input from the user and to store them in the two integer variables. The cout statement in Line 21 is used to display the result on the console.
Analyzing a variable declaration further:
9: int firstNumber = 0;
What this line declares is a variable of type int, which indicates an integer, with a name called firstNumber. Zero is assigned to the variable as an initial value.
The compiler does the job of mapping this variable firstNumber to a location in memory and takes care of the associated memory-address bookkeeping for you for all the variables that you declare. The programmer thus works with human-friendly names, while the compiler manages memory-addressing and creates the instructions for the microprocessor to execute in working with the RAM.
Declaring and Initializing Multiple Variables of a Type
In Listing 3.1, firstNumber, secondNumber, and multiplicationResult are all of the same type—integers—and are declared in three separate lines. If you wanted to, you could condense the declaration of these three variables to one line of code that looks like this:
int firstNumber = 0, secondNumber = 0, multiplicationResult = 0;
Understanding the Scope of a Variable
Ordinary variables like the ones we have declared this far have a well-defined scope within which they’re valid and can be used. When used outside their scope, the variable names will not be recognized by the compiler and your program won’t compile. Beyond its scope, a variable is an unidentified entity that the compiler knows nothing of.
To better understand the scope of a variable, reorganize the program in Listing 3.1 into a function MultiplyNumbers() that multiplies the two numbers and returns the result. See Listing 3.2.
LISTING 3.2 Demonstrating the Scope of the Variables
1: #include <iostream> 2: using namespace std; 3: 4: void MultiplyNumbers () 5: { 6: cout << "Enter the first number: "; 7: int firstNumber = 0; 8: cin >> firstNumber; 9: 10: cout << "Enter the second number: "; 11: int secondNumber = 0; 12: cin >> secondNumber; 13: 14: // Multiply two numbers, store result in a variable 15: int multiplicationResult = firstNumber * secondNumber; 16: 17: // Display result 18: cout << firstNumber << " x " << secondNumber; 19: cout << " = " << multiplicationResult << endl; 20: } 21: int main () 22: { 23: cout << "This program will help you multiply two numbers" << endl; 24: 25: // Call the function that does all the work 26: MultiplyNumbers(); 27: 28: // cout << firstNumber << " x " << secondNumber; 29: // cout << " = " << multiplicationResult << endl; 30: 31: return 0; 32: }
Output
This program will help you multiply two numbers Enter the first number: 51 Enter the second number: 24 51 x 24 = 1224
Analysis
Listing 3.2 does exactly the same activity as Listing 3.1 and produces the same output. The only difference is that the bulk of the work is delegated to a function called MultiplyNumbers() invoked by main(). Note that variables firstNumber and secondNumber cannot be used outside of MultiplyNumbers(). If you uncomment Lines 28 or 29 in main(), you experience compile failure of type undeclared identifier.
This is because the scope of the variables firstNumber and secondNumber is local, hence limited to the function they’re declared in, in this case MultiplyNumbers(). A local variable can be used in a function after variable declaration till the end of the function. The curly brace (}) that indicates the end of a function also limits the scope of variables declared in the same. When a function ends, all local variables are destroyed and the memory they occupied returned.
When compiled, variables declared within MultiplyNumbers() perish when the function ends, and if they’re used in main(), compilation fails as the variables have not been declared in there.
Global Variables
If the variables used in function MultiplyNumbers() in Listing 3.2 were declared outside the scope of the function MultiplyNumber() instead of within it, then they would be usable in both main() and MultiplyNumbers(). Listing 3.3 demonstrates global variables, which are the variables with the widest scope in a program.
LISTING 3.3 Using Global Variables
1: #include <iostream> 2: using namespace std; 3: 4: // three global integers 5: int firstNumber = 0; 6: int secondNumber = 0; 7: int multiplicationResult = 0; 8: 9: void MultiplyNumbers () 10: { 11: cout << "Enter the first number: "; 12: cin >> firstNumber; 13: 14: cout << "Enter the second number: "; 15: cin >> secondNumber; 16: 17: // Multiply two numbers, store result in a variable 18: multiplicationResult = firstNumber * secondNumber; 19: 20: // Display result 21: cout << "Displaying from MultiplyNumbers(): "; 22: cout << firstNumber << " x " << secondNumber; 23: cout << " = " << multiplicationResult << endl; 24: } 25: int main () 26: { 27: cout << "This program will help you multiply two numbers" << endl; 28: 29: // Call the function that does all the work 30: MultiplyNumbers(); 31: 32: cout << "Displaying from main(): "; 33: 34: // This line will now compile and work! 35: cout << firstNumber << " x " << secondNumber; 36: cout << " = " << multiplicationResult << endl; 37: 38: return 0; 39: }
Output
This program will help you multiply two numbers Enter the first number: 51 Enter the second number: 19 Displaying from MultiplyNumbers(): 51 x 19 = 969 Displaying from main(): 51 x 19 = 969
Analysis
Listing 3.3 displays the result of multiplication in two functions, neither of which has declared the variables firstNumber, secondNumber, and multiplicationResult. These variables are global as they have been declared in Lines 5–7, outside the scope of any function. Note Lines 23 and 36 that use these variables and display their values. Pay special attention to how multiplicationResult is first assigned in MultiplyNumbers() yet is effectively reused in main().
Naming Conventions
In case you haven’t noticed, we named the function MultiplyNumbers() where every word in the function name starts with a capital letter (called Pascal casing), while variables firstNumber, secondNumber, and multiplicationResult were given names where the first word starts with a lowercase letter (called camel casing). This book follows a convention where variable names follow camel casing, while other artifacts such as function names follow Pascal casing.
You may come across C++ code wherein a variable name is prefixed with characters that explain the type of the variable. This convention is called the Hungarian notation and is frequently used in the programming of Windows applications. So, firstNumber in Hungarian notation would be iFirstNumber, where the prefix i stands for integer. A global integer would be called g_iFirstNumber. Hungarian notation has lost popularity in recent years in part due to improvements in Integrated Development Environments (IDEs) that display the type of a variable when required—on mouse hover, for instance.
Examples of commonly found bad variable names follow:
int i = 0; bool b = false;
The name of the variable should indicate its purpose, and the two can be better declared as
int totalCash = 0; bool isLampOn = false;