Header Responsive Ads

Variable

what is variable in c++

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

  • Name
  • Data type
  • Size
  • Value

Variable declaration in C++

The syntax of a 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 the name of the 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

The 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 the size of any data-types. For this C++ library provides us a function sizeof(data-type) to check the 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 variables can be declared in a single line as below

int a, b, c, d;

Declare and initialize multiple variables in one line

Multiple variables can be declared and initialized in a 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