Token scanner Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

Six kinds of tokens

The compiler first splits your program into tokens — the smallest pieces that mean something, like words and punctuation in a sentence. Every token is one of six kinds. Spaces, tabs, new lines and comments are not tokens: they only separate tokens, or are thrown away.

keyword      int  return  if  while  for
identifier   total  main  printf  price_2
constant     10  3.14  'A'  0x1F
string       "Hello, World!"
operator     =  +  *  ==  &&  ++
separator    ;  ,  (  )  {  }  [  ]
The 32 keywords of C, grouped by job (all lower-case)
Group Keywords Count You meet them in
Data types char int float double void short long signed unsigned 9 Data, Variables
Qualifiers const volatile 2 Variables, Pointers
Storage classes auto static extern register typedef 5 Scope & storage, Structures
Decisions if else switch case default 5 Decisions
Loops for while do break continue 5 Loops
Jumps return goto 2 Functions, Loops
Your own types struct union enum 3 Your own types
Operator sizeof 1 Variables, Operators

The C character set

  1. Letters: A–Z and a–z. C is case-sensitive, so A and a are different.
  2. Digits: 0–9.
  3. Special characters: + - * / % = < > ! & | ^ ~ ? : ; , . ( ) { } [ ] # ' " \ _
  4. Whitespace: space, tab and new line — they separate tokens.
  5. Other symbols such as @ ` or $ may appear only inside strings and comments.

Rules for identifiers (names)

  1. Use only letters, digits and the underscore _.
  2. The first character must be a letter or _ — never a digit.
  3. No spaces, hyphens or other symbols: my_score, not my-score or my score.
  4. A keyword cannot be a name: int, while, return … are taken.
  5. Upper and lower case differ: total, Total and TOTAL are three names.

Comments, spaces and statements

  1. // comment runs to the end of the line; /* comment */ can cover many lines.
  2. Comments are removed before compiling, and cannot be nested.
  3. Extra spaces, tabs and blank lines are ignored — use them to make code readable.
  4. A statement is one instruction and ends with ; (a semicolon).
  5. { } group statements into a block, e.g. the body of main.
Walkthrough

Every token of a small program

Every line, explained.

1 /* Price of 3 books */ A comment — removed before compiling, so it produces no tokens at all.
2 #include <stdio.h> A preprocessor directive (# include) with a header name <stdio.h>. No semicolon: it is not a statement.
3 int main(void) keyword int · identifier main · separator ( · keyword void · separator ) — 5 tokens.
4 { Separator: opens the block that is the body of main.
5 int qty = 3; keyword int · identifier qty · operator = · constant 3 · separator ; — 5 tokens.
6 float price = 9.5f; 9.5f is ONE token: a floating-point constant with the f (float) suffix.
7 float total = qty * price; // 28.5 Spaces around * are optional; the // comment disappears. 7 tokens.
8 printf("%.1f\n", total); identifier printf · ( · string literal "%.1f\n" (one token, however long) · , · identifier total · ) · ;
9 return 0; keyword return · constant 0 · separator ;
10 } Separator: closes the block.
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Using a keyword as a name

✗ Wrong
int float = 2.5;
int return = 1;
✓ Right
float rate = 2.5;
int result = 1;

Keywords are reserved by C. The compiler stops with "expected identifier".

A name that starts with a digit

✗ Wrong
int 2nd_place = 20;
✓ Right
int second_place = 20;

C reads 2nd_place as a (broken) number: "invalid suffix nd_place on integer constant".

Hyphens or spaces in names

✗ Wrong
int my-score = 5;
int my score = 5;
✓ Right
int my_score = 5;
int myScore = 5;

my-score means "my minus score", and my score is two names. Join words with _ or capital letters.

Capital letters in keywords

✗ Wrong
Int main(void)
{
    Return 0;
}
✓ Right
int main(void)
{
    return 0;
}

All keywords are lower-case. Int and Return are just unknown names to the compiler.

Comments inside comments

✗ Wrong
/* old code:
   x = 5; /* set x */
   y = 6;
*/
✓ Right
/* old code:
   x = 5;  // set x
   y = 6;
*/

The first */ ends the comment, so y = 6; */ becomes code and causes errors. /* comments cannot be nested.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

How many tokens are in: int x = 10;

Question 2

Which is a valid identifier?

Question 3

How many keywords did the original C standard (C89) have?

Question 4

What happens to comments when you compile a program?

Question 5

In "Hello, World!", how many tokens are there?

Assessment

Building Blocks of C quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.