Character Set, concepts of variables and constants

C's character set is its alphabet; a variable is a named memory box whose value can change while the program runs, and a constant is a value that never does.

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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?

  1. marks = 78; written before any declaration of marks
  2. int marks = 78;
  3. marks = 92; written after int marks = 78;
  4. 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).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to C language

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Character Set, concepts of variables and constants · Computer Programming and Programming Methodology (CPPM) · Gri-Learn