C++ Variables
Variables are data name that may be used to store a value. Variable may take different values at different times during execution.
Variables are may refer to some data types which are like int, float, char and etc.
Every variable in C++ language has a specific type that determines the layout and size of the variable’s memory. The ranges of the values that can store with the memory and set of operations that can apply to the variable.

Syntax of variables
type variable_list;
Example of some valid variable declaration
int a,b; char c; float d,month; double k,salary;
Rules for variable declaration
- A variable will have alphabets, digits, and underscore. Example: int _ab,a1,ab;
- No white area is allowed at intervals variable name. Example: int a b;
- A variable name will begin with alphabet and underscore solely. Example: int _ab;
- A variable name should not begin with the digit. Example: int 5;
- A variable name should not be any reserved word or keyword. Example: float, int and etc.

Lower and uppercase letters are distinct because C++ is case-sensitive.
Types of variables
- Local variable
- Global variable
- Static variable
- Automatic variable
- External variable
Local variable
Local variables are used in within the function only, these variables won’t work outside of the function.
void function(){ int a=10; // Local variable }
Global variable
Global variables are used in the entire program, these will work anywhere in the program. These variables are defined outside of the function.
int b=20; // Global variable void function(){ int a=10; // Local variable }
External Variable
External Variable declared with extern keyword. when you declare extern keyword for the variable then it can access in one or more source file.
file.h extern int x=10; //external variable (also global) ------------------------------------------------------- #include #include "file.h" void printValue(){ cout << "Global variable"; cout << x; }
Static Variable
Static Variable is declared with static keyword.
void function1(){ int a=10; //local variable static int b=10; //static variable cout << a << b; b++; }
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.