Using Variables and Declaring Constants in C++
Variables help a programmer temporarily store data for a finite amount of time. Constants help a 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 the keywords auto and constexpr
What Is a Variable?
Before you actually explore the need for and use of variables in a programming language, take a step back and first think about what a computer contains and how it works.
Memory and Addressing in Brief
A computer, smart phone, or other programmable device contains 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 a hard disk. The microprocessor executes instructions comprising your application. In doing so, it uses the RAM to fetch the application binary code to be executed, as well as the data associated with it.
The RAM itself can be visualized as a storage area akin to a row of lockers in a dorm, each locker having a number—that is, an address. To access a location in memory—such as location 578—the processor needs to be programmed using an instruction to fetch a value from the location 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 that result for later use in your program. Instead of painstakingly specifying memory addresses (such as 578) to store the numbers, you would use variables for the purpose. Defining a variable is simple and follows this pattern:
VariableType VariableName;
or
VariableType VariableName = InitialValue;
The VariableType 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 memory where the variable’s value is stored.
Listing 3.1 shows how variables are declared, initialized, and used in a program that multiplies two numbers supplied by the user.
Input ▼
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 multiplies 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 multiplies 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. The program multiplies these two numbers and displays the result. To use numbers entered by the user, it needs to store them in 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. Line 17 performs the multiplication and stores result in variable multiplicationResult. The cout statement in Line 21 is used to display the result on the console.
Let’s analyze a variable declaration further:
9: int firstNumber = 0;
This line declares a variable of type int, which indicates an integer, with the name 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
The scope of a variable is the region of code within which you can use that variable. An ordinary variable like the ones we have declared so far has a well-defined scope within which it’s valid and can be used. When used outside its scope, a variable’s name 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, Listing 3.2 reorganizes the program in Listing 3.1 into a function MultiplyNumbers() that multiplies the two numbers and returns the result.
Input ▼
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 multiplies 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 multiplies 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(), which is 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 such as firstNumber and secondNumber is limited to the function they’re declared in—in this case MultiplyNumbers(). Such variables are therefore called local variables and can be used only within that function.
The curly brace (}) that indicates the end of a function also limits the scope of variables declared after the opening brace. When a function ends, all local variables are destroyed, and the memory they occupy is returned.
In Listing 3.2, therefore, variables declared within MultiplyNumbers() perish when the function ends. 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 MultiplyNumbers() 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.
Input ▼
Listing 3.3 Using Global Variables
1: #include<iostream> 2: using namespace std; 3: 4: // Declare 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 multiplicationResult 21: cout << "Displaying from MultiplyNumbers(): "; 22: cout << firstNumber << " x " << secondNumber; 23: cout << " = " << multiplicationResult << endl; 24: } 25: int main () 26: { 27: cout << "This program multiplies 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 multiplies two numbers Enter the first number: 65 Enter the second number: -3 Displaying from MultiplyNumbers(): 65 x -3 = -195 Displaying from main(): 65 x -3 = -195
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 because they have been declared in Lines 5–7, outside the scope of any function. Note Lines 22–23 and 35–36, which 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 PascalCase), while variables firstNumber, secondNumber, and multiplicationResult were given names where the first word starts with a lowercase letter (called camelCase). 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 convey the type of the variable. This convention is called the Hungarian notation. 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—on mouse hover, for instance.
Examples of commonly found bad variable names follow:
int i = 0; bool b = false;
These variables are considered to be poorly named because their names do not convey their purpose. They would be better named as follows:
int totalCash = 0; bool isLampOn = false;