Difference between Declaration , Definition and Initialization in C++
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
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.
- But the variables may have garbage values.
- Variables can not be used before declaration.
Example
int a,b,c;
Variable Definiton/Initialization
In this stage, the user assigns value as per the requirement within the memory bounds i.e garbage value is overriddenExample
//declaration int a; float b; // definition/initialization later a = 10; b = 78.9;
Example program to demonstrate variable initialization
#include <iostream> using namespace std; int main() { int var; // variable declaration cout << "Value:" << var << endl; // garbage value cout << "Address of var: " << &var << endl; // a's assigned address cout << "Size of var: " << sizeof(var) << " bytes"; // allocated memory in bytes return 0; }
Declaration cum initialization
Variable can be initialized at the time of declaration itselfExample
#include<iostream> using namespace std; int main() { // declaration & initialization at same time int var = 10; float var2 = 10.25; cout << "Value var: " << var << endl; cout << "Value: var2: " << var2 << endl; return 0; }
Output
Value var: 10 Value: var2: 10.25
What are data types?
Data types are used by variables to tell what kind of data it can store. Example – character/integer/decimal etc.
There are three types of data types in C++ –
- Primary
- Derived
- User-Defined
Data Types Modifiers
These are used in conjunction with primitive(built-in) data types to modify the length of data that a particular data type can hold these are –
- Unsigned
- Signed
- Short
- Long
Primitive Data Types (Built-in)
- Boolean
- Character
- Integer
- Floating point
- Double floating point
- Void
- Wide character
Below we have given a list of all primitive data types and their usage –
[table id=550 /]
Below we have given data types and their sizes, limits and types with different modifiers.
[table id=551 /]
C++ Code for Sizes
The following code will give you the output of the size of each data type –// 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
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment