Introduction to C++ variable declaration
Variable declaration in C++ is a part which is done in the starting only to ensure the compiler that there is some variable with the given type and name used in the program so that it can proceed with the further compilation without giving any issues. A variable in C++ is declared before its first use in the program. Declaration of the variable is needed for the compilation time; otherwise, the definition is required at the time of linking the program. In the case of using multiple files, variable declarations are very helpful as the definition is done only once, which will be used while linking the code.
Syntax
Below given is the basic syntax of declaring a variable in a C++ program:
Declaring a single variable in C++
width="624">datatype variable_name;
Declaring multiple variables of the same type at a time in C++, we use commas(,) in between the variable names :
datatype variable1, variable2, variable 3 .... ;
where,
datatype: It defines the data type of the variable which is to be declared. For example, int, float, double, etc.
variable_name: It is the name of the variable which is going to be declared. For example, x, y, num, etc. It can be anything except keywords of C++.
How to declare variables in C++ using various methods?
Types of variables in the C++ depicting their declaration in the code with the help of an example are given below:
1. Local Variable
Local variables are those which are declared inside any particular block of code or a function. The scope of these variables remains only inside that particular block/ function. Otherwise, they have no importance and cannot be used other than the block or function.
Example:
#include <iostream>
using namespace std;
void Employee_salary()
{
// declaration of local variable 'salary'
int salary;
//initialization of local variable 'salary'
salary = 50000;
//salary updation
salary = salary+ 10000;
cout << "Update salary of the employee is " << salary;
}
// Main function
int main()
{
Employee_salary();
}
Output:
2. Global Variable
Global variables are declared outside the program, i.e. outside any block, function, or the main(). They can be accessed anywhere in the entire program, i.e. inside any block, function. These variables only go out of scope when the program exits.
Example:
#include <iostream>
using namespace std;
// declaring the local variable 'emp_name'
string emp_name;
void Employee_data()
{
// using the global variable
cout << "Employee name by default is : " << emp_name<< endl;
}
// Main function
int main()
{
// Initialising the global variable 'emp_name'
emp_name = "Rahul";
cout << "Employee name by default is : " << emp_name << endl;
Employee_data();
}
Output:
3. Static Variable
Static variables are those variables that are declared in the class but outside any function or constructor. One needs to use the keyword ‘static’ while declaring the static variables in the program. Initialization of Static variables is also not mandatory like Instance variables. Since the static variables are created at the starting of the program and get destroyed when the execution of code ends, we cannot access them using the class object.
Example:
#include <iostream>
using namespace std;
class Employee_data {
public:
// declaring the instance variables (used inside the class)
string name;
string department;
int salary;
public:
int salary_update()
{
salary = salary+ ((salary*10)/100);
return salary;
};
};
// Main function
int main()
{
// Creating the object of class 'Employee_data'
Employee_data ed;
// Initialising the instance variables using the class object
ed.name = "Rahul";
ed.department = "IT";
ed.salary = 40000;
// displaying the employee data on the console
cout << "Below given is the Employee data: " << endl;
cout << "Employee name : "<< ed.name<< endl;
cout << "Employee department : "<< ed.department << endl;
cout << "Employee salary after raise : "<< ed.salary_update() << endl;
}
Output:
4. Instance Variable
Instance variables are those variables that are declared inside the class but outside the method or constructor. So they are accessed using the class object. In C++, the initialization of Instance variables is not mandatory. The life of the instance variable is till the object of the class is alive. Once the class object is destroyed, they are also destroyed. They are also known as nonstatic variables.
Example:
#include <iostream>
using namespace std;
class Employee_data {
public:
// declaring the instance variables (used inside the class)
string name;
string department;
// declaring the static variable 'salary'
static int salary;
public:
Employee_data()
{
salary = salary+ ((salary*10)/100);
};
};
//Initialising the static variable ‘salary’
int Employee_data::salary = 40000;
// Main function
int main()
{
// Creating the object of class 'Employee_data'
Employee_data ed;
// Initialising the instance variables using the class object
ed.name = "Rahul";
ed.department = "IT";
// displaying the employee data on the console
cout << "Below given is the Employee data: " << endl;
cout << "Employee name : "<< ed.name<< endl;
cout << "Employee department : "<< ed.department << endl;
cout << "Employee salary after raise : "<< Employee_data:: salary << endl;
}
Output:
Rules and Regulations
Basic rules that need to be followed while declaring a variable in a C++ program are given below:
- Variable names in the C++ program are case sensitive. So one needs to be very specific while declaring a variable. For example, int x and int X are 2 different variables of type ‘int’.
- While declaring a variable, variable names can consist of all uppercase letters ‘A-Z’, lowercase letters ‘a-z’, numbers 0-9.
- C++ Keywords are not allowed in the variable name while declaring a variable.
- Blank spaces are not allowed in the variable name while declaring it.
- Variable names in the declaration can start either with the alphabet or an underscore ( _ ).
- Though one can give any big name to a variable in its declaration, only the first 31 characters are counted, else are ignored by the compiler.
- Programmers can use the ‘extern’ keyword to declare variables in C++ anywhere.
- Variables in C++ can be declared multiple times by the programmer, but they are defined only inside the function or a block.
Conclusion – C++ variable declaration
The above description clearly explains why variables in a program need to be declared and how to declare them in the code. Unlike variable definition, which tells the compiler about how much memory/ storage space is required to store it, the declaration only informs the compiler about the existence of a variable in the program providing the data type and its name. Different variables are declared in the program in their own way. So programmers must understand them properly before working on them.
Recommended Articles
This is a guide to C++ variable declaration. Here we discuss How to declare variables in C++ using various methods along with the examples. You may also have a look at the following articles to learn more –