In C++, a variable is a name given to a memory location that represents the basic unit of storage in a program.
- The value of a variable can be modified during the program's execution, and any operations performed on the variable affect the associated memory location.
- It is mandatory to declare all variables before they are used in a C++ program.
How to Declare Variables?
A typical variable declaration is of the form:
// Declaring a single variable
type variable_name;
// Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers, and the underscore ‘_’ character. However, the name must not start with a number.
Initialization of a variable in C++
In the above diagram,
datatype: Type of data that can be stored in this variable.
variable_name: Name given to the variable.
value: It is the initial value stored in the variable.
Examples:
// Declaring float variable
float simpleInterest;
// Declaring integer variable
int time, speed;
// Declaring character variable
char var;
We can also provide values while declaring the variables as given below:
int a=50,b=100; //declaring 2 variable of integer type
float f=50.8; //declaring 1 variable of float type
char c='Z'; //declaring 1 variable of char type
Rules for declaring variables in C++
- The variable name can contain letters, digits, and underscores.
- The variable name is case sensitive, so 'Arr' and 'arr' are different variables.
- The variable name cannot contain whitespace or special characters such as #,$,%,*.
- All variable names must start with a letter or an underscore (_).
- C++ keywords such as float, double, and class cannot be used as variable names.
Valid variable names
int x; //can be letters
int _yz; //can be underscores
int z40;//can be letters
Invalid variable names
int 89; Should not be a number
int a b; //Should not contain any whitespace
int double;// C++ keyword CAN NOT BE USED
Difference Between Variable Declaration and Definition
The variable declaration refers to the part where a variable is first declared or introduced before its first use. A variable definition is a part where the variable is assigned a memory location and a value. Most of the time, variable declaration and definition are done together.
See the following C++ program for better clarification:
// C++ program to show difference between
// declaration and definition of a
// variable
#include <iostream>
using namespace std;
int main()
{
// Declaration of an integer variable
int x;
// Initialization of the integer variable 'x'
x = 15;
// Definition of the integer variable 'y'
int y = 20;
// Declaration and definition of a character variable 'ch'
char ch = 'Z';
// Definition of a floating-point variable 'f'
float f = 3.14;
// Multiple declarations and definitions
int a, b, c;
// Printing the value of the variable 'ch'
cout << "The value of the character variable ch is: " << ch << endl;
return 0;
}
- This program illustrates the difference between the declaration and definition of a variable in C++.
- A declaration introduces a variable to the compiler, while a definition both introduces a variable and allocates memory for it.
- In the program, the integer variable 'x' is declared and later initialized with the value 15, whereas the integer variable 'y' is defined at the same time it is declared with the value 20.
- Similarly, the character variable 'ch' is declared and defined with the value 'Z', and the floating-point variable 'f' is defined with the value 3.14.
- Finally, multiple variables are declared and defined at once. The program prints the value of the character variable 'ch'.
|
Download the notes
C++ Variables
|
Download as PDF
|
Types of Variables
There are three types of variables based on the scope of variables in C++
- Local Variables
- Instance Variables
- Static Variables
Types of Variables in C++
Let us now learn about each one of these variables in detail.
Variables are essential components of any programming language, and C++ is no exception. In C++, there are three types of variables - Local Variables, Instance Variables, and Static Variables. Let's take a look at each of these types in detail:
- Local Variables: These variables are defined within a block or method or constructor. They are created when the block or function is called and destroyed after exiting the block or when the function returns. The scope of local variables exists only within the block in which they are declared, and they can be accessed only within that block. It is mandatory to initialize local variables.
- Instance Variables: These variables are non-static and are declared in a class outside any method, constructor, or block. They are created when an object of the class is created and destroyed when the object is destroyed. We can use access specifiers for instance variables, and if no access specifier is specified, then the default access specifier will be used. Unlike local variables, it is not mandatory to initialize instance variables, and they can be accessed only by creating objects.
- Static Variables: Static variables are also known as class variables. They are declared using the static keyword within a class outside any method, constructor, or block. Unlike instance variables, we can only have one copy of a static variable per class, irrespective of how many objects we create. They are created at the start of program execution and destroyed automatically when execution ends. It is not mandatory to initialize static variables, and their default value is 0. If we access a static variable through an object, the compiler will show a warning message and replace the object name with the class name automatically. If we access a static variable without the class name, the compiler will append the class name automatically.
|
Take a Practice Test
Test yourself on topics from Software Development exam
|
Practice Now
|
Instance Variable Vs Static Variable
- Each object will have its own copy of the instance variable whereas We can only have one copy of a static variable per class irrespective of how many objects we create.
- Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of the instance variable. In the case of static, changes will be reflected in other objects as static variables are common to all objects of a class.
- We can access instance variables through object references and Static Variables can be accessed directly using the class name.
- The syntax for static and instance variables:
class Example
{
static int a; // static variable
int b; // instance variable
}