Variables, which serve as dynamic data containers in the C++ programming language, are the essential building blocks of programming’s data manipulation and storage. A variable is not the same as a label in memory alone. It acts as a conduit between conceptual concepts and tangible data storage, enabling programmers to work with dataskillfully.
Example:
#include<iostream>
using namespace std;
int main(){
//Variable Ex..
int x = 10;
cout<<“Variable is :”<<x<<endl;
//Identifier Ex..
int y = 50;
cout<<“Identifier is :”<<y<<endl;
return 0;
}
Output is:
Variable is :10
Identifier is :50
Rule for defining variables
There are guidelines for defining variables in C++. The variables you can name must follow these guidelines. The following are the main guidelines:
- The Variable can Capital Letters (A -Z) ,Small Letters (a – z) ,Digits (0 – 9) and Underscore ( _ ).
- Variable names can’t Use Blank Space
- Special Character not allowed i.e. # and $
- Variable names Can be Contain 31 Character’s only.
- Variable can be like int, char, bool, float, double or Void.
Int a;
Example:
123abx is not a variable as it start with digits.
Example of Defining Variables
Int:
#include<iostream>
Using namespace std;
Int main(){
Int = 10;
Return 0;
}
Char:
#include<iostream>
Using namespace std;
Int main(){
char= “Ram”;
Return 0;
}
bool:
#include<iostream>
Using namespace std;
Int main(){
bool= True;
Return 0;
}
float:
#include<iostream>
Using namespace std;
Int main(){
float= 3.14;
Return 0;
}
double:
#include<iostream>
Using namespace std;
Int main(){
double= 25.65;
Return 0;
}
Uses of variables
- Data Storage:
Programmers can store a variety of data types, including characters, integers, and floating-point numbers, using variables.
- Data Manipulation:
After being placed in variables, information can be computed or compared using arithmetic, relational, and logical operators.
- Management of Scope:
Variables can have two distinct scopes: local (seen only within a particular block or function) and global (visible throughout the entire program). Using variables correctly aids with scope management and prevents name conflicts.
- Operations for Input and Output:
Variables are used to hold data that is entered by the user or that is retrieved from other sources, like files. By using conventional input/output streams, they are also utilized to show output to the user.
Scope of variables
Every variable has a range of operation, and beyond that range, they lose their meaning; this range is known as the variable’s scope. The majority of the time, the declaration of a variable occurs inside, not outside, of the curly braces. Although we shall examine the storage classes later, for the time being, we may generally categories variables into two primary types:
- Global Variable
- Local Variable
Global Variable
Global variables are those that can be utilized by any class or function at any point throughout the program’s lifetime after they are declared. They need to be declared outside of the method main method. They can be given different values at different points during the program’s lifetime if they are only declared.
However, they can also be assigned any value at any moment during the program, even if they are simultaneously defined and initialized outside of the main() method.
Example:
#include<iostream>
using namespace std;
//initialized Global variable x = 10
int x = 25;
int main(){
cout<<“Results is:”<< x;
return 0;
}
Output:
Results is: 25
Local Variable
Local variables are those that are declared and only exist in the space between curly braces. They are unavailable outside of that, which causes compile time errors.
Example:
#include<iostream>
using namespace std;
int main(){
// initialization of Local Variable
int x = 20, y = 10, z;
z = x + y;
cout<<“Addition is: “<< z;
return 0;
}
Output is:
addition is: 30