What Is Variable In C++

C++ allows you to store values in variables. Each variable is identified by a variable name.
Additionally, each variable has a variable type.

The type tells C++ how the variable is going to be used and what kind of numbers (real, integer) it can hold.

Variable is just a memory location where we can store value. It’s helps in manipulation. We can reuse this variable for many types. Variables are changeable.

Names start with a letter or underscore ( _ ) followed by any number of letters, digits, or
underscores.

Uppercase is different from lowercase, so the names “sam,” “Sam,” and “SAM”
specify three different variables.

To avoid confusion, it is better to use different names for variables and not depend on case differences.

Most C++ programmers use all lowercase variable names.

Some names, such as int, while, for, and float, have a special meaning to C++ and are considered reserved words.

They cannot be used for variable names.

Syntax

dataType varName;
Example

int x=10;
int abc123;
int a1;
Not valid
int 12a;
int &23abc;

There are many types of variable in c++.

  1. local variable
  2. gloabal vraiable
  3. static variable
  4. auto variable
  5. external variable

Local Variable

A variable that is create and used within function or block is called local variable.

Example Of Local Variable in C++

#include <iostream>
using namespace std;
int main(){
int a=10;//local variable
cout<<“The value of a is:”<<a;
}

Output:
The value of a is: 10

Global Variable

A variable that is create in top of the program and we can use this variable in any function or block that is called a global variable.

Example Of Global Variable In C++

#include <iostream>
using namespace std;
/* /n use here for new line print message*/
void msg(){
int a=10;//global variable
void msg(){
cout<<“The value of a in msg() n”<<a;
}

int main(){
cout<<“The value of a is n”<<a;
msg();
return 0;
}

Output:
The value of a in msg() 10
The value of a is 10

Automatic Variable In C++

auto variable and local variable are same in c programming there is nothing difference between this both are declare within function or block.

Example of Auto Variable In C++

#include <iostream>
using namespace std;
int main(){
int a=10;
auto int b=10;
}

cout<<“The value of a is: %dn”<<a;
cout<<“The value of a is n”<<a;
return 0;
}

Output:
The value of a is: 10
The value of b is: 10

Static Variable In C++

A variable that create with static keyword is called static keyword.It’s helps is memory management normally we can say that it save memory.

Example Of Static Variable In C++

#include <iostream>
using namespace std;
/* endl we are using for print statement in new line same as n */
int a=10; //local variable
static int b=10;//static variable
auto int b=10;
a++;
b++;
cout<<“The value of a is”<<a<<endl;
cout<<“The value of b is”<<b<<endl;
}
int main(){
clrscr();
display();
display();
display();
return 0;
}

Output:
The value of a is 11
The value of b is 11
The value of a is 11
The value of b is 12
The value of a is 11
The value of b is 13

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Variables in c++

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password