Constants/Literals in cpp
Welcome to the whimsical world of programming! In this module, we'll embark on an exciting journey to explore the magical realm of constants and literals in C++. Brace yourself as we unravel the secrets behind these mystical entities that play a pivotal role in maintaining fixed values within your programs. So, grab your coding wand and let's dive in!
Different Types of Constants/Literals in C++
A. Integer constants
Decimal constants : These are numbers expressed in the decimal (base 10) system.
int decimal = 10;
Octal constants : These are numbers expressed in the octal (base 8) system. They are preceded by a '0' (zero).
int octal = 012; // equivalent to decimal 10
Hexadecimal constants : These are numbers expressed in the hexadecimal (base 16) system. They are preceded by '0x' or '0X'.
int hexadecimal = 0x0A; // equivalent to decimal 10
B. Floating-point constants
These are numbers with fractional parts or numbers in scientific notation.
float floatingPoint = 3.14;
double doublePrecision = 2.71828;
C. Character constants
These are individual characters enclosed in single quotes.
char character = 'A';
D. String constants
These are sequences of characters enclosed in double quotes.
std::string str = "Hello, World!";
E. Boolean constants
These represent the truth values, either true or false.
bool trueValue = true;
bool falseValue = false;
F. Null constants
These represent a null or empty value.
int* pointer = nullptr;
Note : C++ also supports other types of constants like wide character constants, wide string constants, and user-defined literals, but the ones mentioned above are the most commonly used ones.
Rules and Syntax for Defining Constants/Literals
A. Naming conventions
- Constants are typically named using uppercase letters with underscores (_) separating words (e.g., MAX_VALUE).
- It's a good practice to use descriptive names that reflect the purpose of the constant.
B. Declaration and initialization
- Constants are declared using the 'const' keyword before the data type.
- Constants must be initialized at the time of declaration and cannot be modified afterward.
- The initialization can be done using an initializer list or an assignment operator (=).
- Constants can be declared globally (outside any function) or locally (inside a function or block).
Examples:
const int MAX_VALUE = 100; // globally declared constant
void function() {
const float PI = 3.14159; // locally declared constant
// You can use the constant in your code
int result = MAX_VALUE * 2;
}
C. Constant qualifiers (const keyword)
- The 'const' keyword is used to declare a constant.
- It ensures that the value of the constant cannot be modified once it is initialized.
- Using 'const' helps improve code clarity and enables the compiler to optimize the program.
Note : Constants/literals in C++ are meant to represent fixed values that should not change during the execution of a program. Using constants enhances code readability, makes it easier to maintain and modify values, and provides compile-time checks for immutability.