Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Variables in C++

2 mins 57 sec read

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 :-

#include 
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.

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor