Variables & Constants in C++


"A variable is a temporary container to store information, it is a named location in computer memory where varying data like numbers and characters can be stored and manipulated during the execution of the program".


Some languages have the mechanism to declare a variable when it is needed without pre-pending by the data type. But C++ has no such mechanism. A variable must be declared before it is used. It is a must because the compiler wants to be aware of it. When declaring a variable, a few naming conventions must be taken in consideration.

  1. It must start with an English alphabet characters, both lowercase and uppercase, including underscore (not a hyphen). It may not start with a digit. The rest is optional. It can either be a letter or a digit (0-9).
  2. C++ keywords like maion, case, class, if, else, do, while, for, tyedef, etc cannot be used as a variable names.
  3. It must be unique within the scope.

There are no restrictions on the length. Legal C++ variable names are hello_world, number but not 1number. C++ is a case sensitive language. The variable names, HELLO, hello, Hello,HellO are treated different.

The underscore “_” enhances the readability of long variable name. The second and the important thing, which has been mentioned, is the uppercase and lowercase letters for variable names. It is at the discretion of programmer to use either one. Some programmers prefer to use a camel notation like my_Age, emp_Salary. Whichever approach is used, it must be consistent throughout the program. The name given to variables is called identifiers. It can be declared at the start of any block of code, but mostly used at the start of the function.

Types of Variables

Basically there are two types of variables, namely

  • Local Variable
  • Global Variable

Local Variable

The local variable is created when the functions gets control and is destroyed when the control transfers back to the caller.

Global Variable

Whereas the Global Variable is known and accessible throughout program. Global Variables are declared at the top of the module.

Declaration of Variable

The declaration of a variable is simple in C++ language. It begins with the data type, followed by at least a space, followed by the name of a variable with a semicolon. Space is optional before the semicolon.

For Example

int my_Age;

int my_Salary;

int _employee;

If different variables are using the same data type on the same line are also allowd, but care must be taken to separate two with a comma except the last one that would end with a semicolon.

For Example

int my_Age,my_Salary,_employee;

Unlike C, declaration in C++ are statements, using this definition, variables may be declared anywhere within the program.

Any legal variable name can be used, it helps to give a sensible name that give you fruitful meaning of what they are being used for. Suppose you are given a short program to add two numbers and display the accumulative value. For this, you should use num1 and num2, for two numbers and a third variable name sum for the result. Always use appropriate names, which can prevent you from extra comments. Remember, C++ programmers tend to prefer short variable names.

Variable attributes

A variable in a programming language represents a storage location. In C++, each variable has six attributes, name, address, type, value, scope and the storage class. The variable name is the label to identify a variable in the source program. The address of a variable is the location in the computer memory occupied by that variable. In case of assignment, a variable name is required on the left side of an assignment statement. The type of a variable refers to the size that is the amount of storage occupied by that variable. To make is simpler, it is the data type associated with that variable.

The scope of the variable depends where it is declared that is local or global. In some cases, a variable exist for short time that is destroyed automatically during the execution of a program but sometimes exists in the entire program. A variable declared with the storage class depends whether it is bound at  compile time or execution time and they are also known as static binding and dynamic binding respectively.

Constants in C++

We know that before the execution, a variable has name and type at execution time, it has a location and value. A constant in C++ means an unchanging value and each constant has a type but does not have location except the string constant.

Integer Constants

Integer constants consist of one or more digits such as 0,1,2,3,4 or -115. Floating point constants contain a decimal point such as 4.15, -10.05. It can also be written in scientific notation such as 1E-35 means 1*10^-35 or -1E35 means 1*10^35.

Character Constants

Character constants specify the numeric value of that particular character such as ‘a’ is  the value of a. Some special constants are in the following table.

Special Character Constants (Escape Sequence)
Constant Description Constant Description
\a Alarm \\ Backslash itself
\b Backspace \' Single Quote itself
\f Form Feed \'' double Quotes itself
\n Line Feed \? question Mark
\r Carriage Return \o Null termination character
\t Horizontal Tab \0 Octal Code
\v Vertical Tab \xh Hexadecimal Code

String Constants

String constants consist of characters enclosed in double quotes such as

“Hello, World”

The string is stored in the memory and the numeric value of that constant is the address of this memory. The string constant is suffixed by ‘\0’, (the null character) by the compiler.

Both C and C++ use escape sequence in the same manner such as ‘\n’ character to produce a new line. All escape sequences are preceded by a backslash, which indicates a special sequence to the compiler. The compiler as a single character views each and every escape sequence.It seems and may have been expected that an escape sequence occupies 2 bytes which is wrong, it occupies only one byte.