Literal reader 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.

Four kinds of literals

A literal is a fixed value written directly in the code. How you write it decides its type: a dot or an e makes a floating-point number, single quotes a character, double quotes a string, and a prefix (0, 0x) or suffix (U, L, LL, f) changes the base or the type.

integer     42    017 (octal)   0x2A (hex)   42U   42L   42LL
floating    3.14  2.5f   1e6   6.02e23   1.5L
character   'A'   '7'   '\n'   '\x41'
string      "Hello"   "Line 1\nLine 2"   ""
Escape sequences — a backslash plus a character
Escape Meaning Code Example
\n new line 10 printf("A\nB") prints A and B on two lines
\t horizontal tab 9 jumps to the next tab stop (every 8 columns)
\\ backslash 92 "C:\\data" is C:\data
\' single quote 39 '\'' is the character '
\" double quote 34 "Say \"Hi\"" prints Say "Hi"
\r carriage return 13 back to the start of the same line
\b backspace 8 cursor one place left
\a alert 7 the terminal beeps
\f form feed 12 new page on a printer
\v vertical tab 11 rarely used
\? question mark 63 only needed in very old code
\0 null character 0 marks the end of every string
\ooo octal code (1–3 digits) — \101 is A (65)
\xhh hexadecimal code — \x41 is A (65)

Integer literals

  1. Decimal: 42. Octal starts with 0: 052. Hexadecimal starts with 0x: 0x2A — all three are 42.
  2. No suffix: the type is int, or a bigger type if the value does not fit.
  3. Suffixes: U = unsigned, L = long, LL = long long (e.g. 5000000000LL, 42UL).
  4. No commas or spaces inside numbers: write 1000000, not 1,000,000.
  5. The minus in -5 is an operator applied to the literal 5.

Floating and character literals

  1. A dot or an exponent makes a floating literal: 3.0, .5, 2e3 (= 2000), 1.5e-3 (= 0.0015).
  2. Without a suffix it is a double. f makes a float (2.5f), L a long double.
  3. A character literal is ONE character in single quotes: 'A', '7', '\n'.
  4. Its type in C is int: 'A' is the number 65, and sizeof('A') is 4.
  5. Escape sequences write special characters: '\n', '\t', '\'', '\\', '\0'.

Strings and named constants

  1. A string literal is text in double quotes; C adds a hidden '\0' at the end.
  2. "Hi" therefore takes 3 bytes: H, i and \0.
  3. Strings written next to each other are joined: "Hello, " "World" is "Hello, World".
  4. const double GST = 0.18; — a read-only variable with a type.
  5. #define GST 0.18 — the preprocessor replaces the name by the text. No = and no ;.
Walkthrough

Every kind of constant in one program

Every line, explained.

1 #include <stdio.h>
2 #define DAYS_IN_WEEK 7 A macro: every DAYS_IN_WEEK becomes 7 before compiling. No = and no ;.
3
4 int main(void)
5 {
6 const double GST = 0.18; A typed, read-only constant. 0.18 is a double literal.
7 long long people = 1400000000LL; LL makes the literal a long long (8 bytes).
8 unsigned int mask = 0xFFu; Hexadecimal FF = 255, and u makes it unsigned.
9 float price = 99.5f; f makes it a float literal; without it, 99.5 would be a double.
10 char grade = 'A'; A character literal: the number 65.
11 printf("%d %.2f\n", DAYS_IN_WEEK, GST); Prints 7 0.18
12 printf("%lld %u\n", people, mask); Prints 1400000000 255 — %lld for long long, %u for unsigned.
13 printf("%.1f %c\n", price, grade); Prints 99.5 A
14 printf("Tab:\t| Quote: \" | Slash: \\\n"); Escape sequences: prints Tab: | Quote: " | Slash: \
15 printf("Hello, " "World!\n"); Two string literals side by side are joined into one: Hello, World!
16 return 0;
17 }
Common mistakes

Spot the bug

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

Commas in numbers

✗ Wrong
int population = 1,000,000;
✓ Right
int population = 1000000;

The comma is an operator in C, not a thousands separator — this does not compile. Write the digits without commas or spaces.

int overflow in a calculation

✗ Wrong
long long ms = 1000 * 60 * 60 * 24 * 365;
// wrong, huge negative
✓ Right
long long ms = 1000LL * 60 * 60 * 24 * 365;
// 31536000000

All the literals are int, so the multiplication is done in int and overflows BEFORE it reaches the long long. One LL makes the whole calculation long long.

A string where a character is needed

✗ Wrong
char grade = "A";
✓ Right
char grade = 'A';

"A" is a string (2 bytes: A and \0), 'A' is one character. A char variable needs single quotes.

Single backslashes in a path

✗ Wrong
printf("C:\new\table.txt");
✓ Right
printf("C:\\new\\table.txt");

\n and \t are escape sequences, so the first prints C:, a new line, ew, a tab and able.txt. Write \\ for one backslash.

= or ; in a #define

✗ Wrong
#define MAX_MARKS = 100;
✓ Right
#define MAX_MARKS 100

Everything after the name is copied in, so MAX_MARKS becomes "= 100;" and breaks the code where it is used.

Practice

Check your understanding

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

0 / 5 correct
Question 1

What is the value of the literal 0x10 ?

Question 2

What is the type of the literal 2.5 (no suffix)?

Question 3

How many bytes does the string literal "Hello" take?

Question 4

Which escape sequence prints a backslash?

Question 5

Which line correctly defines a macro?

Assessment

Constants & Literals quiz

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