Variable
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;
Variable initialization in C++
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 |
Output:
![]() |
Data type size output |
Declare multiple variables in one line
Declare and initialize multiple variables in one line
Multiple variables can be declared and initialized in a single line as below
C++ Program to declare and initialize all data types and display output
Output:
Post a Comment