
Data Type in C++
The type of data a variable can store is defined as a data type of that variable i.e. the data type of the variable determines the size and type of data associated with the variable, for example, an integer data type(int) variable can store integer data, a character data type(char) variable can store character data, etc.
Syntax:
data_type variable = value;
For example,
int a=30;
Here, a is a variable of type int
eger(int). Meaning, the variable can only store either 2 or 4 bytes of integer.

Here are some examples of basic built-in data-types:
Data Type | Meaning | Size (in Bytes) |
---|---|---|
int | Integer | 2 or 4 |
float | Floating-point | 4 |
double | Double Floating-point | 8 |
char | Character | 1 |
wchar_t | Wide Character | 2 |
bool | Boolean | 1 |
void | Empty | 0 |
User-defined and Derived data types will be covered in separate articles.
Variables in C++
Variables are locations of memory for storing data values. It is the basic unit of storage containers in a program.
- The value contained inside a variable can be altered during the execution of a program.
- A variable is only a for a location of memory, all functions done on the variable result in a change of that memory location.
- The variables must be declared before use in C++.
Syntax for declaring a variable:
datatype variable_name = value;
Example:
int myNum = 15; // variable declaration
cout << myNum; // output display
There are three types of variable in C++:
- Local Variables
- Instance Variables
- Static Variables
class GeeksGod
{
public:
static int a; // Static variable
int b; // Instance variable
public:
func()
{
int c; // Local variable
};
};
Local Variable:
A variable defined within a block/method/constructor is known as a local variable. The range of these variables is only within the block in which the variable is declared. That is these variables can be accessed only within that block. Local Variable must be Initialized.
Instance Variable:
They are non-static variables and are declared in a class outside any block, method, constructor.
- Instance Variable may not be Initililized
- Only the objects can access the instance variable hence object creation is must
Static Variable:
Static variables are similar to instance variables, the major difference is that static variables are declared using the static keyword within a class outside any block, method or constructor. A static variable may not be initialized. The standard value of the static variable is 0. They are created at the beginning of execution of a program and terminated automatically when execution of the program ends. Static variables are also known as Class variables.
Constants in C++
Sometimes, depending on our requirements, we need some values that cannot be modified during the execution of the program. So, Constants refer to fixed values that the program cannot alter or change and they are called literals. They can hold any basic data type. A constant is like a variable, having a memory location where a value can be stored. The only difference between a variable and a constant is that a constant cannot change its value during the execution of a program but a variable can change its value during program execution. Initialization of a constant with the word const when it is created is a must.
const int a = 10;
const – Keyword, int– Data Type, a– Name of Constant, 10– Initial Value