Variables in C++ Programming

Variables in C++ Programming
Variables in C++ Programming

Variables in C++: Variable is a name of storage space which is used to store of data. Variable value is changeable. It is a declared always with data types. Variable simple means hold the any value. Variable is most important in every programming languages.

How to define Variable name in C++

C++ variable name must be identified with unique names. It is called variable name. Variable name to define to short name (a and b). But recommend you can define very easy to program based variable name.

Let your C++ program 100 lines of code but variable name proper not define so very hard understand or maintain your code.

Declaration and Initialization of Variable

int id;
float marks;
char grade;

int id=101;
float marks=78.9;
char grade='A';

Note: Here int, float and char is a data type but id, marks and grade are variables name. so variable are most important part of C++ language without variable declared not execute any C++ program.

Rules For Naming variables in C++

1.The first letter of variable should be alphabet or underscore(_).

2. The first letter of variable should not be a digit 

3. After first character it may be combination of alphabets and digits.

4. Blank space are not allowed in variable name.

5. Variable name should not be a keyword.

6.Variable name are case sensitive like JIOCODING or jiocoding are different variables name.

How to Use Variable in C++ Programming

#include<iostream>
using namespace std;
int main(){
int id=110;
float marks=85.6;
char grade='A';
cout<<"Integer Variable of Id:"<<id<<endl;
cout<<"Float variable of Marks:"<<marks<<endl;
cout<<"Character Variable of Grade:"<<grade<<endl;
}
============OUTPUT============
Integer Variable of Id:110
Float variable of Marks:85.599998
Character Variable of Grade:A

Format Specifiers in C++ Language

Data TypeDescriptionUsesSizeFormat Specifier
intintegerwithout decimal4%d
floatfloatingwith Decimal4%f
charCharacterSingle Character1%c
doublefloat doubleLong Double8%lf
boolBooleanTrue and False
%d

Local and Global Variable in C++

Local Variable: Declared inside a function or block, only accessible within that function scope. it is called by local variable.

Example of Local Variable in C++

#include<iostream>
using namespace std;
void local_variable(){
int x=200;
cout<<"I am Local Variable:"<<x;
}
int main(){
local_variable();
}
============OUTPUT============
I am Local Variable:200

Global Variable: A global variable declared outside all functions, accessible outside and inside of  the program. It is called by Global variable.

Example of Global Variable in C++

#include<iostream>
using namespace std;
int x=200;
void local_variable(){
int x=100;
cout<<"I am Local Variable:"<<x<<endl;
}
int main(){
local_variable();
cout<<"I am Global Variable:"<<x;
}

============OUTPUT============
I am Local Variable:100
I am Global Variable:200

Static Variable: A static variable holds its value till the program is running. Use the static keyword and retain values between function calls.

Example of Static Variable in C++

#include<iostream>
using namespace std;
int main(){
for(int i=0;i<=5;i++){
static int x=0;
cout<<"Static Variable:"<<x++<<endl;
}
}
============OUTPUT============
Static Variable:0
Static Variable:1
Static Variable:2
Static Variable:3
Static Variable:4
Static Variable:5

Automatic Variable : Automatic variable are default for local variables, declared with auto keyword.

Example of Automatic Variable in C++

#include<iostream>
using namespace std;
int main(){
auto int x=100;
cout<<"Value of x:"<<x;
}
============OUTPUT============
Value of x:100

External Variable: External variable can be used the extern keyword to access the variables defined in other C++ file. 

Example of External Variable in C++

#include<iostream>
using namespace std;
int x=200;
int main(){
cout<<"Value of x:"<<x<<endl;
extern int x;
x=500;
cout<<"Value of x:"<<x;
}
============OUTPUT============
Value of x:200
Value of x:500

Example of Multiple Assign Variable in C++

#include<iostream>
using namespace std;
int main(){
int a,b,c;
a=b=c=100;
cout<<"Value of a,b,c:"<<a+b+c;
}
============OUTPUT============
Value of a,b,c:300

Conclusion of Variable in C++

Conclusion: A variable is a very important part of C++ language. It can be used in C++ Program. Variable are not define your program not readable or understandable. So variable are used in C++ program.
If any query for variable related you can comments or contact me. I can solve variable related question.


Post a Comment

0 Comments