Difference Between Variable Declaration and Definition
The variable declaration refers to the part where a variable is first declared or introduced before its first use. A variable definition is a part where the variable is assigned a memory location and a value. Most of the time, variable declaration and definition are done together.
See the following C++ program for better clarification:
C++
// C++ program to show difference between // definition and declaration of a // variable #include <iostream> using namespace std; int main() { // this is declaration of variable a int a; // this is initialisation of a a = 10; // this is definition = declaration + initialisation int b = 20; // declaration and definition // of variable 'a123' char a123 = 'a' ; // This is also both declaration and definition // as 'c' is allocated memory and // assigned some garbage value. float c; // multiple declarations and definitions int _c, _d45, e; // Let us print a variable cout << a123 << endl; return 0; } |
a
Time Complexity: O(1)
Space Complexity: O(1)
C++ Variables
Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program.
- The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
- In C++, all the variables must be declared before use.