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

Variable initialization

From Coderwiki
Revision as of 17:17, 9 August 2025 by Dylan (talk | contribs) (add more links)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Variable declaration and variable initialization are two separate terms but are often confused.

What is initialization?

edit edit source

Initialization 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 source

Assignment 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 source
int a;         // declaration: we create the 'a' variable
a = 72;        // initialization: we give 'a' a value
char b = 'b';  // declaration and initialization