Introduction to Constants
In the realm of C++, a constant is a symbol representing a value that cannot be altered during the execution of a program. Constants offer a way to assign meaningful names to fixed values, making your code more readable and maintainable. Let's explore the various types of constants and how they can elevate your coding experience.
- Named Constants : Named constants are declared using the const keyword.
const int MAX_VALUE = 100;
In this example, MAX_VALUE is a named constant with a value of 100. Once set, its value remains unchanged throughout the program. - Enumerations : Enumerations, or enums, are user-defined data types consisting of named integral constants.
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Here, Days is an enumeration with constants representing each day of the week. Constants bring clarity and maintainability to your code by replacing magic numbers with meaningful names.
Introduction to Literals
Literals, on the other hand, are the raw, constant values themselves. They are the building blocks of your program, representing data in its most straightforward form. Let's explore the different types of literals and how they contribute to the rich tapestry of C++. In C++, a constant can be of any basic data types like boolean, character constants, integer constant, strings etc.
For Example : true, 'A', 754, 123.5, "Hello World".Boolean literals in C++
C++ supports two boolean literals "true" and "false". true and false are part of the C++ keywords.
Integer Constants in C++
Integer constants are whole numbers without any fractional part or exponential part. It can either be positive or negative, If no sign precedes then by default it is assumed to be
positive. The range of integer constant depends on programming environment. For Example, In a 16-bit system range of integer literal is -32768 to 32767.
An integer constant can be a decimal, octal, or hexadecimal. A prefix specifies the base of number system.
Prefix | Description | Base | Example |
---|---|---|---|
0x or 0X | Hexadecimal Number | 16 | 0x5C, 0x22 |
0 | Octal Number | 8 | 012C, 0243 |
Nothing | Decimal Number | 10 | 25, 100 |
You can also specify the type of integer constant by using a suffix character. It can be lowercase or uppercase and can be in any order.
- L, l : Long
- U, u : Unsigned
- Unsigned Integer Constant : 100U, 565U
- Long Constant : 67L, -2398876L
- Unsigned Long Constant : 55UL, 77652UL
- Decimal Constants : 85, -54
- Octal Constants : 0213, 045
- Hexadecimal Constants : 0x4b, 0x2A
Character Constants in C++
Character constants are enclosed between a pair or single quote. C++, supports two type of character constants, wide character literal and narrow character literal. If a character literal starts with uppercase 'L' then it is a Wide character (for Example : L'a') and must be stored in a variable of type wchar_t otherwise it is a narrow character(for Example : 'a') and can be stored in variable of type char. The ASCII value of character constants are stored internally.
For Example : 'a', 'A', '1', '#' are character constants. C++ Backslash Character ConstantsThere are some characters which are impossible to enter into a string from keyboard like backspace, vertical tab, newline etc. C++ includes a set of backslash character which have special meaning in C++ programming language. The backslash character ('\') changes the interpretation of the characters by C++ compiler.
Escape Sequence | Description |
---|---|
\” | Double quote(") |
\' | Single quote(') |
\\ | Backslash Character(\) |
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\a | Alert or bell |
\? | Question mark |
\0 | Null character |
#include <iostream> using namespace std; int main() { char c1 = 'E'; wchar_t wc = L'E'; cout << c1 << endl; cout << wc << endl; // Printing newline character cout << '\n'; printf("Tech\nCrash\nCourse"); return(0); }
Output
E 69 Tech Crash Course
C++ Floating-point Constants
A floating point constant has a integer part, a decimal point, a fractional part and may contain an exponential part. Floating point literal may be positive or negative but must have a decimal point. You can also represent floating point literals in scientific notation.
For Example: 1234.5432, -100.001, 2.37E-3C++ String Constants
p class="myParagraph"> A string constant in C++ is a set of characters enclosed between a pair of double quotes. A string literal is actually a character array. A string literal may contain any number of characters including alphanumeric characters, escape characters, graphical characters etc. For Example- "" : Null String.
- "A" : String literal having one characters.
- "XYZ12.iyu" : String literal with multiple characters.
- "XYZ jjuh\n" : String with spaces and escape characters.
Declaration of Constants in C++
We can define constants in C++ in two ways.- Using const keyword.
- Using #define preprocessor directives.
const float INTEREST_PERSENTAGE = 9;
#include Preprocessor Directive
We can use #define preprocessor directive to define a constant as per following syntax.
#define INTEREST_PERSENTAGE 9 C++ Program to show use of #include preprocessor
#include <iostream> #define PI 3.141 using namespace std; int main() { float radius; cout << "Enter Radius of Circle\n"; cin >> radius; cout << "Circumference of Circle : " << 2*PI*radius; return 0; }
Output
Enter Radius of Circle 5.0 Circumference of Circle : 31.41
Hexadecimal and Octal Constants
- Hexadecimal Constants : Hexadecimal constants are prefixed with 0x or 0X and use digits 0-9 and letters A-F.
int hexValue = 0xA3;
Here, 0xA3 is a hexadecimal constant. - Octal Constants : Octal constants are prefixed with 0 and use digits 0-7.
int octalValue = 075;
In this example, 075 is an octal constant. Using hexadecimal and octal constants can be advantageous in certain scenarios, especially when dealing with low-level programming and bitwise operations.
Conclusion
You've successfully navigated the rich terrain of constants and literals in C++. From understanding the purpose of constants and their role in enhancing code readability to exploring the various types of literals and their nuances, you're now equipped with the knowledge to wield these tools effectively in your programming endeavors.
As you continue your programming journey, remember that constants and literals are not just elements of syntax; they are storytellers within your code, conveying meaning and precision. Use them wisely to craft code that not only functions flawlessly but also communicates clearly.