Literals
Literals are the actual values that are directly written into your code to represent specific data. They are used to provide initial values for variables, as operands in expressions, or as direct values in statements.
C++ supports different types of literals:
1) Integer Literals: These represent whole numbers, and they can be written in decimal, octal, or hexadecimal formats.
int decimal = 42; // Decimal integer literal
int octal = 052; // Octal integer literal (0 prefix)
int hex = 0x2A; // Hexadecimal integer literal (0x prefix)
2) Floating-Point Literals: These represent real numbers and can be written in decimal or exponential notation.
double decimalNum = 3.14159; // Decimal floating-point literal
double exponentNum = 6.02e23; // Exponential floating-point literal
3) Character Literals: These represent single characters and are enclosed in single quotes.
char ch = 'A'; // Character literal 'A'
char newline = '\n'; // Character literal for newline
4) String Literals: These represent sequences of characters and are enclosed in double quotes.
const char* greeting = "Hello, World!"; // String literal
5) Boolean Literals: C++ has two boolean literals, true and false, which represent the Boolean values.
bool isTrue = true;
bool isFalse = false;
Difference Between Constant and Literals
In this article, we are going to discuss the differences between Constant and Literals in a programming language with examples.
Contact Us