2.3. Variables

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a named location that stores a value.

Just as there are different types of values (integer, character, etc.), there are different types of variables. When you create a new variable, you have to declare what type it is.

Note

In C++, integer variables are declared as type int and character variables are declared as type char.

The following statement creates a new variable named fred that has type char.

char fred;

This kind of statement is called a declaration.

The type of a variable determines what kind of values it can store. A char variable can store a character, and it should come as no surprise that int variables can store integers.

There are several types in C++ that can store string values, but we are going to skip that for now (see Chapter 7).

To create an integer variable, the syntax is

int bob;

where bob is the arbitrary name you made up for the variable. In general, you will want to make up variable names that indicate what you plan to do with the variable. For example, if you saw these variable declarations:

char first_letter;
char last_letter;
int hour, minute;

you could probably make a good guess at what values would be stored in them. This example also demonstrates the syntax for declaring multiple variables with the same type: hour and minute are both integers (int type).

Although the language allows this, it should be avoided. It is easy to accidentally create uninitialized variables, one of the most common sources of error in C++ programs. Consider the following:

Bad variable declaration
1#include <iostream>
2
3int main () {
4    int hour, minute = 59;
5    std::cout << hour << ':' << minute;
6}

What do you think this program will output?

What should it output?

Details

Uninitialized variables are an example of undefined behavior.

The C++ language does specify what should happen here. Most compilers will warn that you did this, but none will stop you from compiling and running this program unless you ask it to.

The value of hour could be 0, but might also be any value constructed from the bits that just happened to be stored in memory when hour was created.

Many compilers will allow something along the lines of:

-Werror=uninitialized

To force an error if an uninitialized variable is detected.

Naming conventions

C++ is a multi-paradigm language with a long history. Over the years many different groups have developed different programming styles for C++. None of them are wrong as long as they apply their rules consistently.

Since this book intends to teach C++, it follows the conventions used in the C++ Standard Library. For more information about naming conventions, see the Naming conventions section of the C++ Core Guidelines, edited by Bjarne Stroustrup and Herb Sutter.

If you continue programming in C++, you will undoubtedly encounter variables that use other styles.

That's ok. You can't follow everyone's conventions.

Q1

A(n) is a name given to a location in memory used to keep track of a value.

Q2

Take a look at the code block below:

char tom;

It's an example of a(n) statement.

Q3

Click on all instances of integer VARIABLES.


    int  = ;
    int  = ;
     = '';
    while ( < ) {
        cout << c << '\n';;
        ++;
    }
    ouble d = ;
    int  =  + ;
    cout << "It's the year " <<  +  << '!';
}

Q4

Click on all instances of character VARIABLES.

int main() {
    har  = '';
    string  = ;
    out <<  << '' <<  << '\n';
    string  = ;
    char  = '';
    cout <<  << '' <<  << '\n';
    string c = ;
    cout << c;
}

Q5

Match the variable to the kind of value it can store.

Q6

Write code that creates the variables name, first_initial, and number_of_siblings IN THAT ORDER. It is up to you to choose the correct types for these variables.

  1. char first_initial
  2. char first_initial;
  3. double number_of_siblings
  4. double number_of_siblings;
  5. int number_of_siblings
  6. int number_of_siblings;
  7. string first_initial
  8. string first_initial;
  9. string name
  10. string name;


More to Explore