The variable is the basic unit of storage that holds the value while the program is executed. We can also say that it is a name given to the memory location. A variable is defined by data-types provided by C++.
These variables belongs to the types in c++ which are the following:
int
: stores integers (whole numbers), such as 120 or -120.double
: single-precision floating-point value with decimals, such as 29.99 or -29.99.char
: stores single characters, such as ‘a’ or ‘B’ within single quotes.string
: stores text within double-quotes such as “Hello World”.bool
: stores true or false values.void
: Represents the absence of type.wchar_t
: A wide-character type.
Rules for naming C Variables:
- A variable name must begin with a letter or underscore.
- They are case sensitive that is Score and score are not the same.
- They can be constructed with digits, letters and underscore.
- No special symbols are allowed other than underscores.
- A variable name cannot be a keyword. Example,
int
cannot be a variable name as it is an in-built keyword.
Defining a variable:
Defining a variable means the compiler has to now assign storage to the variable because it will be used in the program. You can directly define a variable inside the main() function and use it.
To define a function we must provide the data type and the variable name. We can even declare multiple variables of the same data type in a single line by using a comma to separate them.
type variable_list_name
, where type must be the valid c++ data-type.
Example:
1 2 3 4 | int i; char c, ch; //multiple variable in single line float f, amount, salary; double db; |
Declaration of Variables
The declaring variable is useful for multiple files and the variable is declared in one of the files and is used during linking the file.
Declaration of variables must be done before they are used in the program. Declaration does the following things.
- It tells the compiler what the variable name is.
- It specifies what type of data the variable will hold.
A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. Keyword extern
is used for the declaration of variables at any place in the program.
1 2 | extern int a; extern float b, c; //multiple declaration |
Initialization of Variables
initialization is simply giving the variable a certain initial value which may be changed during the program.
A variable can be initialized and defined in a single statement, syntax:
1 | data_type variable1_name = value1, variable2_name = value2; |
Example:
1 2 3 4 5 6 | int a; // declaration a = 10; // initialization //can be done in single step too int a = 20; float x = 20.5, y = 11.25; //multiple initialization |
NOTE: If a variable is declared and not initialized by default it will hold a garbage value.
If you declare the variable which is already been declared then it will throw a compile time error.
Example:
1 2 3 4 | int x, y; //declaration x = 10; //initialization y = 20; //initialization int j = i+j; //compile time error |
C++ Program to demonstrate the declaration, definition and initialization of vaariables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; // Variable declaration: extern int x, y; extern int z; extern float f; int main() { // Variable definition: int x, y; int z; float f; // variable initialization x = 10; y = 20; z = x + y; cout << z << endl; f = 19.0 / 3.0; cout << f << endl; return 0; } |
Output:
1 2 | 30 6.33333 |