Difference between Declaration , Definition and Initialization in C++
February 18, 2023
Declaration, Definition and Initialization
Here, on this page, we will discuss the declaration, definition and initialization of a variable. A variable is a memory unit that is capable of storing data which can be modified(rewritten) at any point of time in a program. Simply a variable is a name given to a memory location.
Declaration , Definition and Initialization in C++
A variable may have the following –
Variable Declaration
Variable definition (initialization)
In C++, all the variables must be declared before use.
Let us look at both of them in detail –
Variable Declaration & Definition
NoteNote - The definition of declaration/definition is given wrong on g4g.
Declaration
Variable declaration is the notification to the program/programmer that a particular type of memory may be required and we plan to call that memory with some name.
Memory creation (as per specified datatypes) happens at the time of declaration itself.
// C++ program to sizes of data types
#include<iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << " byte" << endl;
cout << "Size of int : " << sizeof(int) << " bytes" << endl;
cout << "Size of short int : " << sizeof(short int) << " bytes" << endl;
cout << "Size of long int : " << sizeof(long int) << " bytes" << endl;
cout << "Size of signed long int : " << sizeof(signed long int) << " bytes" << endl;
cout << "Size of unsigned long int : " << sizeof(unsigned long int) << " bytes" << endl;
cout << "Size of float : " << sizeof(float) << " bytes" << endl;
cout << "Size of double : " << sizeof(double) << " bytes" << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" << endl;
return 0;
}
The output of the code depending on the default sizes of data types and modifiers in your compiler will be –
Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
Understanding Each Datatype
We will look at all different data types in C++ :
Integer (int)
Uses to store integer values like : -200, 150, 6812 etc
Usual Range – it can store values from -2147483648 to 2147483647
Usual Size – 4 bytes(some older compilers may support 2 bytes)
int age = 25;
Float and Double
Float and double are used to store floating-point numbers (decimals and exponentials)
The size of float is 4 bytes
The size of the double is 8 bytes.
Double has two times the precision of float.
Datatype Range Macro
float 1.17549e-38 to 3.40282e+38 FLT_MIN
float(negative) -1.17549e-38 to -3.40282e+38 -FLT_MIN
double 2.22507e-308 to 1.79769e+308 DBL_MIN
double(negative) -2.22507e-308 to -1.79769e+308 -DBL_MIN
An example of initializing variables would be –
float val1 = 21.25;
double val2 = 1531.24595;
double val3 = 21.34E14 // 45E12 is equal to 21.34 * 10^14
double val4 = 1.23E-12 // 45E12 is equal to 1.23 * 10^-12
Char
Its size is 1 byte.
Characters in C++ are enclosed inside single quotes ' '.
For example –
char c = 'a';
char val = ‘5’; // 5 stored as ascii character rather than int
Bool
Has a size of 1 byte
Used to store true/false values
Also used in conditional operations
Example –
bool val = false;
wchar_t
Wide character wchar_t is similar to the char data type
However, its size is 2 bytes instead of 1
These are used to represent characters that need more memory than 1 byte due to a large number of unique characters
Example –
wchar_t test = L'א' // storing Hebrew character;
Void
Void is used with functions and pointers
They mean that they do not have any data type of value
We will learn more about it later
We cannot declare variables of the void type.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment