Header Ads

Variable

Variable is a container that reserve memory from RAM according to its data type to store value. There are many types of variables. Variable must have:-

  • Name
  • Data type
  • Size
  • Value

Variable declaration in C++

Syntax of variable declaration is below

Data-type variable-name = value that variable hold;


int number;

Variable initialization in C++

int number = 12;

Here int is data-type, number is name of variable, 12 is the value this variable hold and its size is 4 bytes as size of int data-type is 4 bytes.

Variable data-types (Types of Variables) and sizes

Size of a variable may be different depending on the compiler and the computer you are using. Common data types and size are given below:-

 Type         size
 char     1 byte    
 int     4 bytes
 short     2 bytes
 float 4 bytes
 double 8 bytes
 string     8 bytes
 boolean     1 byte


Write a C++ Program to check size of any data-types. For this C++ library provides us a function sizeof(data-type) to check size of all data types.

data type size
Data type size

Output:

data type size output
Data type size output

Declare multiple variables in one line

Multiple variable can be declared in single line as below

int a, b, c, d;

Declare and initialize multiple variables in one line

Multiple variable can be declared and initialized in single line as below

int a=12, b, c=4, d=12;

C++ Program to declare and initialize all data types and display output 

data types cpp

Output:

data types cpp output


Post a Comment

Previous Post Next Post

In Article Ads 1 Before Post

In Article Ads 2 After Post