Variable (identifier)


A variable is a location in your computer’s memory in which you can store a value and from which you can later retrieve that value.
To understand this, you must first understand a bit about how computer memory works. Your computer’s memory can be thought of as a series of cubby holes, all lined up in a long row. Each cubby hole—or memory location—is numbered sequentially. These numbers are known as memory addresses.
Variables not only have addresses, they have names. For example, you might create a variable named myVari. Your variable is a label on one of these cubby holes so that you can find it easily, without knowing its actual memory address.
The RAM (Random access memory):
When you define a variable in C++, you must tell the compiler what kind of variable it is (this is usually referred to as the variable’s “type”.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++.
Rules for naming a C++ variable (identifier)
1. Variable name should start with an alphabet (letter) or an underscore.
2. Variable name may contain more than one character. Second characters onwards we may use only alphabets or digit or underscore.
3. No special characters are allowed in a variable name except underscore.
4. A variable name in C++ is case sensitive. Uppercase and lowercase letters are distinct.
Sum, sum, SUM and sUm are treated as four different variable names in C++.
5. A variable name cannot be a keyword.
void, char, int, float, double, if and else are incorrect variable names because they are keywords.
6. In Borland C++ only first 55 characters in a variable name are significant.
Examples of correct variable names are given below:
marks, m1, m2, Father_Name, Max_Score, sub1code, ans, Roll, INT, Char, _Val, _Input_Screen, CompScMarks
Generally a C++ variable name does not start with an underscore (_). List of incorrect variable names are given below:
Creating variable
A variable is a name given to a memory location to store a value and it represents a value in a program. The value assigned to the variable name may change during execution of program. The program can always access the current value of the variable by referring to its name.
Rule: DataType VariableName;
DataType VariableName1, VariableName2, VariableName3, … ;
Creating   a variable is a statement in C++ and every C++ statement is terminated by a   semi-colon (;). String is not a fundamental data type but still examples are   given how to create string variables. An array of character is used to create   a string variable. An example is given below:char name[30];More   detailed discussion about array and strings will be done later.
char sex;
char ans, choice, section;
char name[30];
char subject[20], country[25];
int roll;
int flatno, number, cellno, phone;
float average;
float area, length, marks;
double temperature;
double radius, price, rate;
Memory Allocation
Every variable in C++ is allocated fixed amount of memory. C++ data types, memory allocation and range of values are given below:
Date   Type
Storage   (Memory Allocation)
Range   of values
1 byte   or 8 bits
-128 to   127
4 bytes   or 32 bits
-2147483648   to 2147483647
4 bytes   or 32 bits
3.4*10-38   to 3.4*1038
8 bytes   or 64 bits
1.7*10-308   to 1.7*10308
Assignment Operator
Value is assigned to a variable by using assignment operator. Using assignment operator, value is stored in a variable when writing a program. Using assignment operator, value is copied to a variable.
Rule: VariableName = Value;
DataType VariableName = Value;
Usage of assignment operator
char ch;
int roll;
double rate;
Usage of assignment operator
char ch=’A’;
int roll=13;
double rate=154.25;
unsigned short Width;
Width = 5;
You can combine these steps and initialize Width when you define it by writing
unsigned short Width = 5;
Initialization looks very much like assignment, and with integer variables, the difference is minor. Later, when constants are covered, you will see that some values must be initialized because they cannot be assigned to. The essential difference is that initialization takes place at the moment you create the variable.
Just as you can define more than one variable at a time, you can initialize more than one variable at creation. For example:
// create two long variables and initialize them
long width = 5, length = 7;
This example initializes the long integer variable width to the value 5 and the long integer variable length to the value 7. You can even mix definitions and initializations:
int myAge = 39, yourAge, hisAge = 40;
This example creates three type int variables, and it initializes the first and third.

Next : C++ Constant