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

Assignment: Difference between revisions

From Coderwiki
new
 
(No difference)

Latest revision as of 17:16, 9 August 2025

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

What is assignment?

edit edit source

Assignment is when we set the value of a variable.

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 initialization?

edit edit source

Initialization 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