C++ Variables reserved memory location or space of memory for store value .Integer is a whole number and Interger written as in c++ is int. Type and identify are two type variable define.
variable is really just the name of the program's operational memory area. Each variable in C++ has a specified type. The type determines the size and layout of the variable store. Values in this range can be stored in memory, and operators can be applied to variables.
The name of a variable can consist of letters, numbers, and underscore characters. It must start with a letter or an underscore. Uppercase and lowercase letters are different because C++ is case sensitive.for example int myVariable = 5 ; Finally we can say data type name for variable is int.
There are following basic types of variable in C++ as explained in last chapter
|Sr.No||Type & Description|
Stores either value true or false.
Typically a single octet (one byte). This is an integer type.
The most natural size of integer for the machine.
A single-precision floating point value.
A double-precision floating point value.
Represents the absence of type.
A wide character type.
C++ also allows you to define various other types of variables, such as enumerations, pointers, arrays, references, data structures, classes, etc., which will be explained in subsequent chapters.
Below we will explain how to define, declare and use various types of variables.
Variable declarations in C++
The variable declaration guarantees to the compiler that the variable exists with the given type and name so that the compiler can continue to compile further without knowing the full details of the variable. A variable declaration has its meaning only at compile time, and the compiler needs the actual variable declaration when the program is connected.
Variable declarations are useful when you use multiple files and define variables only in one of them (files that define variables are available when the program is connected). You can declare a variable anywhere with the extern keyword. Although you can declare a variable multiple times in a C++ program, variables can only be defined once in a file, function, or code block.
Declaration and Initialization
Variable must be declared before they are used. Usually it is preferred to declare them at the starting of the program, but in C++ they can be declared in the middle of program too, but must be done before using them.
int i; // declared but not initialised char c; int i, j, k; // Multiple declaration
Initialization means assigning value to an already declared variable,
int i; // declaration i = 10; // initialization
Initialization and declaration can be done in one single step also,
int i=10; //initialization and declaration in same step int i=10, j=11;
If a variable is declared and not initialized by default it will hold a garbage value. Also, if a variable is once declared and if try to declare it again, we will get a compile time error.
int i,j; i=10; j=20; int j=i+j; //compile time error, cannot redeclare a variable in same scope
Lvalues and Rvalues in C++
There are two types of expressions in C++:
- lvalue: An expression that points to a memory location is called an lvalue expression. The lvalue can appear to the left or right of the assignment number.
- Rvalue: The term rvalue refers to the value of some addresses stored in memory. An rvalue is an expression that cannot be assigned to it, that is, the rvalue can appear to the right of the assignment number, but not to the left of the assignment number.
variable is an lvalue, so it can appear to the left of the assignment number. Numeric literals are right-valued and therefore cannot be assigned and cannot appear to the left of an assignment number. Here is a valid statement:
int g = Span> 20;
But this is not a valid statement and will generate a compile-time error:
10 = 20;