Variables in C++

2 mins 57 sec read Basic

What are Variables in C++?

Like human memory , Computer memory has millions of cells where it can store data or information. Now to retreive an information from those cells , each cell should be given one name called as 'variable name'. These variables are mostly useful and can be of different types like integer, float , double , char, strings.

What is variable declaration ?

Variable declaration means we just create a variable and memory will hold some space for that variable. We did not given any value yet just declared a variable.

Example :-
int a;
int b;
float c;

What is variable definition ?

Variable definition means we assigned some value to a variable.

Example :-
int a = 10;
int b = 20;
int c = 30;
float d = 12.0;
char a = "A";

What happens in memory when we declare a variable ?

int a =10;
int b = 20;
int c = 30;


Types of variables


There are two types of variables :

1) Local variables

2) Global variables

What are local variables ?

Local variables persist value only inside a block. If you try to use a local variable outside a block of function or method you will see a compile error.

Example :-

using namespace std;
/* Global value a = 20; */ 
int a = 20;
int main() {

  cout<<"Global value of a is "

Output :-

Global Value of a is 20
Local value of a is 10

Variable 'a' assigned with value 20 is global variable. That means it is visible in the entire program . Anywhere in any function you can access it. But more closer you are more priority you will get . This is the philosophy of variables :) Inside a main function we assigned a variable 'a' with value 20 thats why it's next print statement printed the value 20.

How was the tutorial? Nice. Right?

Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.

What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp

Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python