Types of variables in C++

A variable in programming is just like a variable in the mathematics, it is used to store some data in it, which we can manipulate in our code. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Variables follow the same naming convention as we have discussed in identifiers tutorial.
There are following basic types of variable in C++.
Type
Description
bool
Stores either value true or false.
char
It is used to store a byte of data.
int
The most natural size of integer for the machine.
float
A single-precision floating point value.
double
A double-precision floating point value.
void
Represents the absence of type.
There are also some other types of variables which will be discussed in later tutorials.
Following section will cover how to define, declare and use various types of variables.
Variable Definition in C++
A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type, and contains a list of one or more variables of that type as follows −
type_of_variable name;
Here, type_of_variable can be any of the above discussed type. Name can be any legal or can be list of names separated by a comma for the same type. Some valid declarations are shown here −
int    a, b;
float income;
double d;
The line int a, b; both declares and defines the variables a and b, which instructs the compiler to create variables of type int with named a and b.
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:
type_of_variable name = value;
Some examples are:

int d = 3;
byte z = 22;        
char x = 'x';

Comments

Popular posts from this blog

Data types in c++

Comments in C++