Home > Articles

Using Variables and Declaring Constants in C++

Learn how to use memory to store values temporarily in variables and constants in C++.
This chapter is from the book

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;

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020