Elementary Programming in C++: Identifiers and Variables

1. Identifiers

Identifiers are the names that identify elements such as variables and functions in a program.

As you see in Listing 2.3, main, numberl, number2, number3, and so on are the names of things that appear in the program. In programming terminology, such names are called identi­fiers. All identifiers must obey the following rules:

■    An identifier is a sequence of characters comprising letters, digits, and underscores (_).

■    An identifier must start with a letter or an underscore; it cannot start with a digit.

■    An identifier cannot be a reserved word. (See Appendix A, “C++ Keywords,” for a list of reserved words.)

■   An identifier can be of any length, but your C++ compiler may impose restriction. Use identifiers of 31 characters or fewer to ensure portability.

For example, area and radius are legal identifiers, whereas 2A and d+4 are illegal identi­fiers because they do not follow the rules. The compiler detects illegal identifiers and reports syntax errors.

2. Variables

Variables are used to represent values that may be changed in the program.

As you see from the programs in the preceding sections, variables are used to store values to be used later in a program. They are called variables because their values can be changed.

In the program in Listing 2.2, radius and area are variables of the double-precision, floating-point type. You can assign any numerical value to radius and area, and the val­ues of radius and area can be reassigned. For example, in the following code, radius is initially 1.0 (line 2) and then changed to 2.0 (line 7), and area is set to 3.14159 (line 3) and then reset to 12.56636 (line 8).

Variables are used to represent data of a certain type. To use a variable, you declare it by telling the compiler its name as well as the type of data it can store. The variable declaration tells the compiler to allocate appropriate memory space for the variable based on its data type. The syntax for declaring a variable is

datatype variableName;

Here are some examples of variable declarations:

int count;                     // Declare count to be an integer variable

double radius;                 // Declare radius to be a double variable

double interestRate;           // Declare interestRate to be a double variable

These examples use the data types int and double. Later you will be introduced to addi­tional data types, such as short, long, float, char, and bool.

If variables are of the same type, they can be declared together, as follows:

datatype variable1, variable2,…, variablen;

The variables are separated by commas. For example,

int i, j, k; // Declare i, j, and k as int variables

Variables often have initial values. You can declare a variable and initialize it in one step. Consider, for instance, the following code:

int count = 1;

This is equivalent to the next two statements:

int count;

count = 1;

You can also use shorthand to declare and initialize variables of the same type together. For example,

int i = 1, j = 2;

Every variable has a scope. The scope of a variable is the part of the program where the scope of a variable variable can be referenced. The rules that define the scope of a variable will be introduced gradually later in the book. For now, it’s sufficient to know that a variable must be declared and initialized before it can be used.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *