Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Variable declaration

From Coderwiki
Revision as of 21:27, 8 August 2025 by Dylan (talk | contribs) (Variable declaration: rename example section to 'Example: declaration and initialization in C')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

While the two terms are often used synonymously, there is a difference between variable declaration and variable initialization.

What is declaration?

edit edit source

Declaration is the process of creating a variable.

When we declare a variable, we essentially create it. We don't give it a value, only say that it exists.

Of course, it will be given some sort of initial value, as it allocates the memory and there must be something at that memory location. It's just that most programming languages will give the variable a value such as undefined, null or possibly through a 'variable not defined' error or exception if you try to access the data. Other languages like C may just return garbage data if you try to access its value.

Example: declaration and initialization in C

edit edit source
int a;         // declaration: we create the 'a' variable
a = 72;        // initialization: we give 'a' a value
char b = 'b';  // declaration and initialization