In C++, literals are fixed values that appear directly in the code. Literals represent constant values that the compiler interprets at compile-time. There are various types of literals in C++, including numeric literals, character literals, and string literals.
Here’s a breakdown of each type:
1. Numeric Literals
Numeric literals represent constant numerical values and can be classified as integer literals or floating-point literals.
- Integer Literals: These represent whole numbers and can be written in decimal, octal, or hexadecimal form.
- Decimal: Written as usual numbers, e.g., `42`, `0`, `1000`
- Octal: Starts with `0`, e.g., `075` (interpreted as octal 75, which is decimal 61)
- Hexadecimal: Starts with `0x` or `0X`, e.g., `0x2A` (interpreted as hexadecimal 2A, which is decimal 42)
- Suffixes (optional): `u` for unsigned (e.g., `10u`), `l` for long (e.g., `10l`), and `ll` for long long (e.g., `10ll`).
- Floating-point Literals: Represent numbers with decimal points or in exponential notation.
- Decimal: Written with a decimal point, e.g., `3.14`, `0.001`
- Exponential Notation: Uses `e` or `E` for the exponent, e.g., `1.5e2` (which equals `1.5 10^2` or `150`)
- Suffixes (optional): `f` for float (e.g., `3.14f`), `l` for long double (e.g., `2.718l`)
Example:
int decimal = 42;
unsigned int octal = 075; // Octal representation (decimal 61)
int hex = 0x2A; // Hexadecimal representation (decimal 42)
float pi = 3.14f;
double large = 1.5e3; // Equivalent to 1500.0
---
2. Character Literals
Character literals represent individual characters and are enclosed in single quotes (`' '`).
- Character Literal: A single character enclosed in single quotes, e.g., `'A'`, `'1'`, `'@'`
- Escape Sequences: Used to represent special characters, like newline (`\n`) or tab (`\t`).
- Examples: `'\n'` (newline), `'\t'` (tab), `'\''` (single quote)
- Unicode Characters: Represented with `\u` followed by a 4-digit hex code, e.g., `'\u00A9'`
Example:
char letter = 'A';
char newline = '\n';
char unicodeChar = '\u03A9'; // Unicode character for Ω (Omega)
3. String Literals
String literals represent sequences of characters and are enclosed in double quotes (`" "`).
- Basic String: A sequence of characters within double quotes, e.g., `"Hello, World!"`
- Escape Sequences: Can also be used within string literals, e.g., `"Line 1\nLine 2"`
- Multiline String: Not directly supported in C++, but can be achieved using concatenation, e.g., `"This is line 1 " "and this is line 2."`
- Raw String Literal: Allows special characters like `\n` or `\\` without escape sequences by using `R"()"` syntax.
Example:
std::string greeting = "Hello, World!";
std::string multiline = "Line 1\nLine 2"; // Includes a newline
std::string rawString = R"(C:\path\to\file.txt)"; // Treats backslashes as literals
Summary
Literal type | Example | Notes |
---|---|---|
Integer Literals | `42`, `0x2A`, `075`, `100u` | Whole numbers, with optional suffixes `u`, `l`, or `ll` |
Floating-point Literals | `3.14`, `1.5e2`, `2.718f`, `3.14 | Decimal or scientific notation, optional suffix `f` or `l` |
Character Literals | `'A'`, `'\n'`, `'\u03A9'` | Single characters, with escape sequences |
String Literals | "Hello"`, `"Line 1\nLine 2"`, `R"(Raw Text)" | Sequences of characters, optional raw strings |
- Log in to post comments