Theory
The program with a memory problem
Your marks program proudly prints Marks: 78. Then the next test happens and Riya scores 92.
Your only option so far: open the source, edit the text, recompile, rerun. For one student. Sixty students, weekly tests... you would be a full-time typist.
The missing idea is a box: a place in memory where the program KEEPS a value, looks at it, and changes it while running. C calls that box a variable, and it is the single most used idea in all of programming.
Theory
Labelled dabbas and an engraved nameplate
Think of a kitchen shelf of steel dabbas, each with a label: sugar, tea, elaichi. The label stays; the contents change all month. That is a variable: named box, replaceable contents. Beside the shelf hangs an engraved nameplate: the shop's name, fixed at installation, never repainted. That is a constant. C programs are shelves of labelled dabbas plus a few engraved plates.
Theory
First, the alphabet: C's character set
Before words, an alphabet. C source code may use only its character set:
- Letters: A to Z, a to z
- Digits: 0 to 9
- Special symbols:
+ - * / % = < > ( ) { } [ ] ; : , . # & ! ?and friends - Whitespace: space, tab, newline
Everything you will ever type in a C program, variable names, numbers, operators, is spelled from this set. (Hindi or Gujarati letters can live inside quoted text for display, but the code itself speaks this alphabet.)
Practical
The marks program gets its first boxes
#include <stdio.h>
int main() {
int marks = 78; /* declare + initialize */
printf("Marks: %d\n", marks);
marks = 92; /* assignment: new contents */
printf("After retest: %d\n", marks);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Variables and constants, formally
A variable is a named memory location whose value can change during execution. Three moments in its life:
- Declaration:
int marks;reserves the box and fixes what it holds (an int). - Initialization:
int marks = 78;declares AND puts the first value in. - Assignment:
marks = 92;replaces the contents (the old 78 is gone).
A constant never changes: the literals themselves (78, 3.14, 'A'), or named ones: const int PASS = 35; (locked box) and #define (coming two lessons ahead).
Think first
Predict the output
Read the code block above once more, line by line. Exactly what two lines does it print?
Show the answer
Marks: 78
After retest: 92
The second printf prints 92, not 78: assignment replaced the box's contents before the second look. A variable always shows its current contents, and the old value is not stored anywhere. (%d is a placeholder that prints an int, formally introduced soon.)
Quiz
Which line will the C compiler REJECT?
- marks = 78; written before any declaration of marks
- int marks = 78;
- marks = 92; written after int marks = 78;
- int marks; followed later by marks = 78;
Show the answer
marks = 78; written before any declaration of marks
C demands declaration before use: assigning to a box that was never declared is a compile error ("undeclared identifier"). The other three are the legal life cycle: declare+initialize, reassign, or declare first and fill later. Declaration-before-use is a favourite spot-the-error exam line.
Watch out
The two beginner bites
Using before declaring: the compiler stops with "undeclared identifier", C never guesses a box into existence. Reading `=` as "equals": in C, = means assign, put the right side INTO the left box. marks = 92 is an action, not a claim. (The question "are these equal?" uses ==, and confusing the two is THE classic C bug, formally hunted in the operators lesson.)
Theory
Where this goes next
Now that boxes exist, three questions follow, one lesson each: what may a box be named (identifiers, keywords, next), what types of boxes exist (int vs float vs char), and what operations combine them (operators). By the end of this unit the marks program will take input, compute a percentage, and print a report, all with these boxes.
Summary
Key takeaways
- C's character set: letters, digits, special symbols, whitespace, the code's alphabet.
- A variable is a named memory box whose value can change during execution.
- Declaration reserves the box; initialization fills it first; assignment replaces contents.
- Declaration before use, always: undeclared names are compile errors.
- Constants never change: literals (78, 'A') and named constants (const, #define).
- Memory hook: labelled dabbas (variables), engraved nameplate (constants).