Constants and literals let you use fixed values in your program that do not change. This helps keep your code clear and safe.
Constants and literals in C++
const type name = value; // Examples: const int DAYS_IN_WEEK = 7; const double PI = 3.14159;
const keyword makes a variable constant, so its value cannot change after initialization.
Literals are fixed values like numbers (42), characters ('a'), or strings ("hello") used directly in code.
const int MAX_SCORE = 100;
const char NEWLINE = '\n';
const double TAX_RATE = 0.07;
const char* const GREETING = "Hello!";
This program shows how to declare constants and use them in output. It also shows that trying to change a constant causes an error.
#include <iostream> int main() { const int DAYS_IN_WEEK = 7; const double PI = 3.14159; const char NEWLINE = '\n'; std::cout << "Days in a week: " << DAYS_IN_WEEK << NEWLINE; std::cout << "Value of PI: " << PI << NEWLINE; std::cout << "This is a newline character: " << NEWLINE; // Uncommenting the next line will cause a compile error because DAYS_IN_WEEK is constant // DAYS_IN_WEEK = 8; return 0; }
Constants must be initialized when declared.
Trying to change a constant value after initialization will cause a compile-time error.
Literals are the actual fixed values you write in your code, like numbers or text.
Constants hold fixed values that cannot change during program execution.
Literals are the actual fixed values written directly in code.
Using constants makes your code safer and easier to understand.
Practice
constant in C++?Solution
Step 1: Understand the meaning of constant
A constant in C++ is a value that once set cannot be changed during program execution.Step 2: Compare options with definition
A fixed value that cannot be changed after initialization matches this definition exactly, while others describe variables or functions.Final Answer:
A fixed value that cannot be changed after initialization -> Option AQuick Check:
Constant = fixed value [OK]
- Confusing constants with variables
- Thinking constants are functions
- Assuming pointers are constants
MAX_SIZE with value 100 in C++?Solution
Step 1: Recall syntax for constant declaration
In C++, the keywordconstbefore the type declares a constant.Step 2: Check each option
const int MAX_SIZE = 100; usesconst int MAX_SIZE = 100;which is correct. int MAX_SIZE = 100; missesconst, C is incomplete without initialization, D uses invalid keyword.Final Answer:
const int MAX_SIZE = 100; -> Option DQuick Check:
Useconstbefore type for constants [OK]
- Omitting 'const' keyword
- Using 'constant' instead of 'const'
- Not initializing the constant
const int x = 5; int y = 10; std::cout << x + y << std::endl;
Solution
Step 1: Understand the code variables
Variablexis a constant with value 5,yis 10.Step 2: Calculate the expression output
The expressionx + yequals 5 + 10 = 15, which is printed.Final Answer:
15 -> Option BQuick Check:
5 + 10 = 15 [OK]
- Thinking constants cause errors
- Concatenating numbers as strings
- Expecting runtime errors
const int a = 10; a = 20; std::cout << a << std::endl;
Solution
Step 1: Analyze constant assignment
The variableais declared as constant with value 10.Step 2: Check reassignment
Trying to assign 20 toais illegal because constants cannot change value.Final Answer:
Cannot assign a new value to a constant variable -> Option CQuick Check:
Constants cannot be reassigned [OK]
- Trying to reassign constants
- Ignoring compiler error messages
- Confusing constants with variables
Solution
Step 1: Understand constant literal requirements
The value should be fixed, unchangeable, and precise (double is better than float).Step 2: Evaluate options
const double pi = 3.14159; declares a constant double with initialization, which is best practice. float pi = 3.14159; is not constant, C uses macro which is less safe, D misses initialization.Final Answer:
const double pi = 3.14159; -> Option AQuick Check:
Use 'const double' with initialization for constants [OK]
- Using non-const variables for constants
- Using #define instead of const
- Not initializing constants
Ready to prove it?
Pass the test and this pattern moves unknown
