Variable declaration in C++

Variable declaration in C++
Techiio-author
Written by Sagar RabidasFebruary 3, 2022
11 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Today in this blog, we will discuss the Variable declaration in C++.

C++ variable declaration

The variable assertion in C++ is a part that is accomplished within the starting handiest to make certain the compiler that there is some variable with the given kind and call used inside the application so that it may continue with the in addition compilation without giving any troubles. A variable in C++ is asserted earlier than its first use within the software. An announcement of the variable is needed for the compilation time; in any other case, the definition is needed at the time of linking the program. In the case of the usage of a couple of files, variable declarations are very useful as the definition is executed most effectively once, on the way to be used even as 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:

Local Variable

Nearby variables are the ones that are declared internal any particular block of code or a characteristic. The scope of these variables stays the handiest internal that particular block/ characteristic. In any other case, they don't have any importance and cannot be used aside from 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();
}

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();
}

Static Variable

Static variables are those variables that are declared within the magnificence but outside any feature or constructor. One wishes to apply the keyword ‘static’ whilst affirming the static variables within the program. Initialization of static variables is likewise not obligatory like instance variables. Since the static variables are created at the beginning of this system and get destroyed while the execution of code ends, we can't get the right of entry to them using the magnificence item.

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;
}

Instance Variable

Instance variables are those variables that are declared within the elegance however outside the method or constructor. So they're accessed using the magnificence item. In C++, the initialization of example variables isn't obligatory. The existence of the example variable is until the item of the elegance is alive. Once the magnificence item is destroyed, it may be additionally destroyed. They're additionally 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;
}

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.

C
C++
Variable declaration
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance