C Workshop · Declare and Assign
Lesson C

Declare and Assign

Declaration names the box. Assignment fills it.

What it is

Declare means “please reserve a box and give it this name.” Assign means “write this value into that box.” They are two different jobs. This lesson does them on two separate lines so you can see each one.

Approach

  1. Declare int n;
  2. Assign n = 42;
  3. Print with %d and a newline.

Where this is used in the real world

Diagram

1

int n;

declare — the box exists but is empty

Reading n now would be undefined behavior.
2

n = 42;

assign — write a value into the box

The name n now means 42.
3

printf

print the stored value

Output is 42.

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3  int n;
4  n = 42;
5  printf("%d\n", n);
6  return 0;
7}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. What does int n; do by itself?

Declare without initialize.

2. Which line writes 42 into the box?

Assignment.

3. Why is assign-before-printf required?

Do not read uninitialized.

4. int n = 42; is:

Combined initialization.

5. %d is the format for:

Decimal int.