A variable is a storage area that is used to store data. The variable contains data that can be changed at any time during program execution.
Variable This is the name of a memory location. Variable is an identifier is used to represent a particular type of information in a part of a program. We can see a Variable as a container is used to store a value. Variable This is the name of a memory location.
INDEX –
- Variables in c++ programming
- rules of declaring variables in c++ programming
- Variable Declaration
- Definition
- Variable initialization
- Types of variable
- Global variable
- Local variable
- Conclusion
- Frequently asked questions
Variables in c++ programming –
rules of declaring variables in c++ programming
Variable is case sensitive. for eg int a and int, A these are different variables.
A Variable starts with any alphabet (a-z, A-Z) or underscore (_).
Variables can be named alphanumeric. For eg. a1 = 5, var1, var2 For eg. a1 = 5, var1, var2
Variable does not allow space.
The Variable name is not any C ++ keywords.
After declaring a variable, it is given a value. This value is given by the equal sign (=).
Like – x = 5; Or a = 10;
Each data type has a variable
Syntax –
- Without assigning value:- <data type> <variable name>;
- With assigning value:- <data type> <variable name> = <variable value>;
data type | Example | Description |
int | a = 2; | The int data type is used to declare numeric values. |
char | ch = ‘H’; and str[]=”hello”; | We use the char data type to declare the character. |
float | f = 2.3; | Float data type is used to declare variables like floating-point, but they are single-precision. |
double | d = 2.30; | Double data type is used to declare variables like floating-point, but they are double-precision. |
bool | b = true; or b = (!false) | There are two values for bool. True and False |
wchar_t | str[] = L”Hello”; | This is a wide character data type. |
Datatype Example Description
int – int a = 2; The int data type is use to declare numeric values.
Char- char ch = ‘H’; and char str [ ] = “hello”; Use the char data type to declare the character.
Float – float f = 3.3; Float data type is used to declare variables like floating-point, they are single precision.
Double – double a = 3.20; Double data type is used to declare variables like floating-point, they are double precision.
Bool– holds boolean value true or false
Variable Declaration –
Variable declares, this variable allocates memory according to the data type.
After being a variable declared, it takes Garbage Value inside itself.
Garbage Value – Garbage Value Variable is given by Compiler.
ever using a variable it has to declare.
Syntax for Single Declaration
data_type single_variable_name;
Example – int a;
#include<iostream> using namespace std; int main(){ int a; cout<<“Value of a : “<<a; return 0; } |
Output –
Syntax for Multiple Variable Declaration
data_type multiple_variable_name;
Example – int a, b, c;
#include<iostream> using namespace std; int main(){ int a, b, c; cout<<“Value of a : “<<a<<endl; cout<<“Value of b : “<<b<<endl; cout<<“Value of c : “<<c<<endl; return 0; } |
Output –
Definition –
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.
Initialization –
Variable initializes, this variable allocates memory according to the data type it is. For eg. int for 2bytes (16-bit) | 4bytes (32-bit) | 8bytes (64-bit), char
In variable initialization, a variable takes only one value.
The Syntax for Single Initialization
data_type single_variable_name = value;
Example – int a = 8;
#include<iostream> using namespace std; int main(){ int a = 8; cout<<“Value of a : “<<a; return 0; } |
Output –
Syntax for Multiple Variable Initialization
data_type single_variable_name = value, single_variable_name = value;
Example – int a=5, b=6;
#include<iostream> using namespace std; int main(){ int a = 5, b = 6; cout<<“Value of a : “<<a<<endl; cout<<“Value of b : “<<b; return 0; } |
Output –
Variable Redeclaration –
Variable cannot be redeclare.
Example –
#include<iostream> using namespace std; int main(){ int a, b; int b; cout << “Value of a : ” <<a; return 0; } |
Output –
Types of variable
1. Global variable
2. Local variable
1. Global variable –
Global variables are those variables. we can access the entire program variables that are defined at the beginning of the program.
A global variable is a variable that declares outside of functions. The global variable can be used in all functions.
The visibility of Global Variables occurs throughout the program.
The default value of Global Variables is ‘0’
Example –
#include<iostream.h> #include<conio.h> // number is a Global variable int number=50; void seo() { cout<<“Your Number is : “<<number; } void main() { clrscr(); seo(); getch(); } |
Output –
2. Local variable –
The default value of Local Variables is ‘garbage value‘.
The local variables are variables that are declared inside a function. These local variables can only be used within the function or block in which these are declared.
The use of such variables remains limited to block only.
for example, you have created a variable in a function, then you cannot use a variable outside function.
Example –
#include<iostream.h> #include<conio.h> void seo() { // number is a local variable int number=20; } void main() { // Error: because access to local variable cout<<number; getch(); } |
Output –
In the code, int variable is declare seo(). Whose name is number.
this variable is Access in the main () function. the error will happen in the program num is a local variable. It can only use in seo ()
Difference between Global variable and Local variable in c++
Global variable | Local variable |
Global Variable is called variable which is defined at the top of the program declared outside the function. | Local Variable is the variable is declared inside the function. |
Global Variable can be accessed anywhere | Local Variable can be accessed only inside the function in which that variable is declared. |
Global variable has initial value int = 0, float = 0.000000, char- “Blank Space (Non-Printable Char)” | initial value of Local variable is garbage. |
The scope of Global Variable means that its availability persists throughout the program, | the scope of Local Variable means that availability remains until the function or block is executed. |
The data segment of a Global variable is stored in the area which is the public area. | the data of Local Variable is stored in the stack. |
read also – control statements
Some special types of variable
There are some special keywords, to impart unique characteristics to the variables in the program.
Final –
Sometimes you don’t want to allow derived class to override the base class’ virtual function. C++ 11 allows a built-in facility to prevent overriding of a virtual function using the final specifier.
final int i=1;
Static –
These variables hold their value between function calls. Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in a static storage area. And they have a scope till the program lifetime.
static int y=2;
source –wikipedia
Conclusion –
Variable types, Variable Declaration, Variable Definition, Variable Initialization all of them Syntax, example, output I gave you above.
Friends, reading this post completely, you must have understood Variables in c++ programming. you must have liked it, I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post this post helped you share your friends.
Frequently Asked Questions
A variable is a storage area is used to store data.
The variables in C ++ are tow types.
Local Variables, Global Variables
Variable does not allow space.
Yes, Each data type has a variable.
Garbage Value Variable is given by Compiler.
The default value of Local Variables is ‘garbage value’.