Variable initialization
From Coderwiki
More actions
Variable declaration and variable initialization are two separate terms but are often confused.
What is initialization?
edit edit sourceInitialization is when we give a variable a value.
In most statically-typed languages, we must first declare (create) the variable before we can give it a value. In some languages like Python, however, we can initialize any variable without needing to first declare it.
How does it differ from assignment?
edit edit sourceAssignment is also the process of giving a variable a value. So what's the difference?
Assignment is whenever we change the value of any variable, while initialization is only when it is first given a value.
This means that initialization is a type of assignment.
Example: declaration and initialization in C
edit edit sourceint a; // declaration: we create the 'a' variable
a = 72; // initialization: we give 'a' a value
char b = 'b'; // declaration and initialization