PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Look at the source token card: 42, 3.5, 'A', "A", true. Each item is a literal because the value is written directly in the source code. You can recognize a literal by looking at the token itself, without needing a name around it. The first two may look like the obvious cases, but the quote marks and the spelling of true also belong to literal syntax.
A literal is not defined by looking like a number. 42 writes one value directly, and so do 3.5, 'A', "A", and true. The written form tells you that the source contains a value rather than a name that needs to be interpreted elsewhere.
Which items on the token card are literals?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The spelling of each token card item places it in a literal category. 42 is an integer literal because it is written as a whole number. 3.5 is a floating-point literal because it includes a decimal point. 'A' is a character literal, "A" is a string literal, and true is a boolean literal.
| SOURCE TOKEN | LITERAL CATEGORY |
|---|---|
| 42 | Integer literal |
| 3.5 | Floating-point literal |
| 'A' | Character literal |
| "A" | String literal |
| true | Boolean literal |
These categories come from the written forms, not from how long the token looks or how familiar it seems. The two A tokens contain the same visible letter, but their quote marks give them different categories. The lowercase word true is also a literal because that exact source spelling represents a boolean value.
In 'A', the single quotes are part of the character literal syntax. They mark one character, A. In "A", the double quotes are part of the string literal syntax. They mark text containing A. The quote marks are not optional decoration around the same token, so exchanging them changes the literal category.
'A'
"A"The two tokens therefore cannot be treated as identical spellings. 'A' is the character literal on the card, while "A" is the string literal on the card. Keeping the quote marks in the source is what lets each token retain its intended category.
Replace the token "A" with the exact token for one character.
"A"Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Small spelling changes on the token card have concrete effects. Removing the decimal point from 3.5 produces 35, an integer literal with a different value category. Removing or exchanging the quotes around A changes the intended character or string form, and can leave a token that is not the intended literal. The marks around A are part of what the source says.
The boolean literal must use the lowercase spelling true. Changing it to True changes the token, so it is no longer the intended boolean literal. A literal's punctuation and capitalization are meaningful source characters, not formatting choices that can be adjusted without consequence.