Constants & Literals
Values written in the codeA literal is a value written straight into the code: 42, 0x1F, 3.5f,
'A', "Hi". The way you write it decides its type and its size.
In the lab below you see how C reads any literal you type, print escape sequences on a real terminal,
and compare const with #define.
- 3.5is a double · 3.5f a float
- 'A'is an int with the value 65
- "Hi"takes 3 bytes (hidden \0)
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
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 | 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
- Decimal: 42. Octal starts with 0: 052. Hexadecimal starts with 0x: 0x2A — all three are 42.
- No suffix: the type is int, or a bigger type if the value does not fit.
- Suffixes: U = unsigned, L = long, LL = long long (e.g. 5000000000LL, 42UL).
- No commas or spaces inside numbers: write 1000000, not 1,000,000.
- The minus in -5 is an operator applied to the literal 5.
Floating and character literals
- A dot or an exponent makes a floating literal: 3.0, .5, 2e3 (= 2000), 1.5e-3 (= 0.0015).
- Without a suffix it is a double. f makes a float (2.5f), L a long double.
- A character literal is ONE character in single quotes: 'A', '7', '\n'.
- Its type in C is int: 'A' is the number 65, and sizeof('A') is 4.
- Escape sequences write special characters: '\n', '\t', '\'', '\\', '\0'.
Strings and named constants
- A string literal is text in double quotes; C adds a hidden '\0' at the end.
- "Hi" therefore takes 3 bytes: H, i and \0.
- Strings written next to each other are joined: "Hello, " "World" is "Hello, World".
- const double GST = 0.18; — a read-only variable with a type.
- #define GST 0.18 — the preprocessor replaces the name by the text. No = and no ;.
Every kind of constant in one program
Every line, explained.
#include <stdio.h>
#define DAYS_IN_WEEK 7
A macro: every DAYS_IN_WEEK becomes 7 before compiling. No = and no ;.
int main(void)
{
const double GST = 0.18;
A typed, read-only constant. 0.18 is a double literal.
long long people = 1400000000LL;
LL makes the literal a long long (8 bytes).
unsigned int mask = 0xFFu;
Hexadecimal FF = 255, and u makes it unsigned.
float price = 99.5f;
f makes it a float literal; without it, 99.5 would be a double.
char grade = 'A';
A character literal: the number 65.
printf("%d %.2f\n", DAYS_IN_WEEK, GST);
Prints 7 0.18
printf("%lld %u\n", people, mask);
Prints 1400000000 255 — %lld for long long, %u for unsigned.
printf("%.1f %c\n", price, grade);
Prints 99.5 A
printf("Tab:\t| Quote: \" | Slash: \\\n");
Escape sequences: prints Tab: | Quote: " | Slash: \
printf("Hello, " "World!\n");
Two string literals side by side are joined into one: Hello, World!
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Commas in numbers
int population = 1,000,000;
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
long long ms = 1000 * 60 * 60 * 24 * 365; // wrong, huge negative
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
char grade = "A";
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
printf("C:\new\table.txt");
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
#define MAX_MARKS = 100;
#define MAX_MARKS 100
Everything after the name is copied in, so MAX_MARKS becomes "= 100;" and breaks the code where it is used.
Check your understanding
Answer each question — you will see the explanation straight away.
What is the value of the literal 0x10 ?
0x means hexadecimal: 1×16 + 0 = 16.
What is the type of the literal 2.5 (no suffix)?
Floating literals without a suffix are double. 2.5f would be a float.
How many bytes does the string literal "Hello" take?
Five characters plus the hidden '\0' at the end = 6 bytes.
Which escape sequence prints a backslash?
\\ is one backslash. \b is backspace.
Which line correctly defines a macro?
#define NAME text — no = and no semicolon, because the text is copied in exactly.
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.