C++ DEVELOPER SITE
PYTHONPHPJAVA
 

C++ Variables


C++ Variables reserved memory location or space of memory for store value .

Integer is a whole number and Interger written as in c++ is int. Type and identify are two type variable define. for example int myVariable = 5 ; Finally we can say data type name for variable is int.

There are following basic types of variable in C++ as explained in last chapter
Sr.No Type & Description
1

bool

Stores either value true or false.

2

char

Typically a single octet (one byte). This is an integer type.

3

int

The most natural size of integer for the machine.

4

float

A single-precision floating point value.

5

double

A double-precision floating point value.

6

void

Represents the absence of type.

7

wchar_t

A wide character type.


Declaration and Initialization

Variable must be declared before they are used. Usually it is preferred to declare them at the starting of the program, but in C++ they can be declared in the middle of program too, but must be done before using them.

Example :

int i;      // declared but not initialised
char c; 
int i, j, k;  // Multiple declaration

Initialization means assigning value to an already declared variable,

int i;   // declaration
i = 10;  // initialization

Initialization and declaration can be done in one single step also,

int i=10;         //initialization and declaration in same step
int i=10, j=11;

If a variable is declared and not initialized by default it will hold a garbage value. Also, if a variable is once declared and if try to declare it again, we will get a compile time error.

int i,j;
i=10;
j=20;
int j=i+j;   //compile time error, cannot redeclare a variable in same scope