Bird
Raised Fist0
C++programming~10 mins

Constants and literals in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constants and literals
Start
↓
Define literal value
↓
Assign to constant variable
↓
Use constant in code
↓
Attempt to change constant?
Yes→Error: Cannot modify constant
No↓
Program runs using constant
↓
End
This flow shows how a literal value is assigned to a constant variable, which cannot be changed later in the program.
Execution Sample
C++
const int days_in_week = 7;
int main() {
  int total_days = days_in_week * 4;
  return total_days;
}
This code defines a constant for days in a week and calculates total days in 4 weeks.
Execution Table
StepActionVariableValueNote
1Define constant days_in_weekdays_in_week7Constant assigned literal 7
2Enter main function--Program starts main
3Calculate total_daystotal_days284 * days_in_week = 4 * 7 = 28
4Return total_days-28Program returns 28
5Attempt to modify days_in_weekdays_in_weekErrorCannot modify constant - compile error
💡 Program ends after returning total_days; constant cannot be changed
Variable Tracker
VariableStartAfter Step 1After Step 3Final
days_in_weekundefined777
total_daysundefinedundefined2828
Key Moments - 2 Insights
Why can't we change the value of days_in_week after defining it?
Because days_in_week is declared as a constant (const), the compiler prevents any modification after initialization, as shown in step 5 of the execution_table.
What is the difference between a literal and a constant?
A literal is a fixed value written directly in code (like 7), while a constant is a variable that holds a literal or value and cannot be changed, as seen in step 1 where 7 is assigned to days_in_week.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of total_days after step 3?
A4
B7
C28
DUndefined
💡 Hint
Check the 'Value' column in row with Step 3 in execution_table.
At which step does the program try to modify the constant and cause an error?
AStep 5
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step mentioning 'Attempt to modify days_in_week' in execution_table.
If we change the literal 7 to 8 in the constant definition, what will total_days be after step 3?
A28
B32
C7
D8
💡 Hint
total_days = 4 * days_in_week; changing days_in_week to 8 means 4*8=32.
Concept Snapshot
Constants and literals in C++:
- Literal: fixed value like 7
- Constant: variable declared with 'const' that cannot change
- Syntax: const type name = value;
- Using constants prevents accidental changes
- Compiler error if you try to modify a constant
Full Transcript
This lesson shows how constants and literals work in C++. A literal is a fixed value like 7. A constant is a variable declared with 'const' that holds a literal or value and cannot be changed later. The example defines days_in_week as a constant with value 7. Then it calculates total_days as 4 times days_in_week. The program returns 28. If you try to change days_in_week later, the compiler gives an error. This protects important values from accidental changes.

Practice

(1/5)
1. Which of the following best describes a constant in C++?
easy
A. A fixed value that cannot be changed after initialization
B. A variable that can change its value anytime
C. A function that returns a fixed value
D. A pointer to a variable

Solution

  1. Step 1: Understand the meaning of constant

    A constant in C++ is a value that once set cannot be changed during program execution.
  2. 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.
  3. Final Answer:

    A fixed value that cannot be changed after initialization -> Option A
  4. Quick Check:

    Constant = fixed value [OK]
Hint: Constants never change value after being set [OK]
Common Mistakes:
  • Confusing constants with variables
  • Thinking constants are functions
  • Assuming pointers are constants
2. Which of the following is the correct way to declare a constant integer named MAX_SIZE with value 100 in C++?
easy
A. int MAX_SIZE = 100;
B. constant int MAX_SIZE = 100;
C. int const MAX_SIZE;
D. const int MAX_SIZE = 100;

Solution

  1. Step 1: Recall syntax for constant declaration

    In C++, the keyword const before the type declares a constant.
  2. Step 2: Check each option

    const int MAX_SIZE = 100; uses const int MAX_SIZE = 100; which is correct. int MAX_SIZE = 100; misses const, C is incomplete without initialization, D uses invalid keyword.
  3. Final Answer:

    const int MAX_SIZE = 100; -> Option D
  4. Quick Check:

    Use const before type for constants [OK]
Hint: Use 'const' keyword before type to declare constants [OK]
Common Mistakes:
  • Omitting 'const' keyword
  • Using 'constant' instead of 'const'
  • Not initializing the constant
3. What is the output of the following C++ code?
const int x = 5;
int y = 10;
std::cout << x + y << std::endl;
medium
A. Runtime error
B. 15
C. Syntax error
D. 510

Solution

  1. Step 1: Understand the code variables

    Variable x is a constant with value 5, y is 10.
  2. Step 2: Calculate the expression output

    The expression x + y equals 5 + 10 = 15, which is printed.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Add constant and variable values normally [OK]
Common Mistakes:
  • Thinking constants cause errors
  • Concatenating numbers as strings
  • Expecting runtime errors
4. Identify the error in the following C++ code:
const int a = 10;
a = 20;
std::cout << a << std::endl;
medium
A. Incorrect use of std::cout
B. Missing semicolon after declaration
C. Cannot assign a new value to a constant variable
D. Variable 'a' not declared

Solution

  1. Step 1: Analyze constant assignment

    The variable a is declared as constant with value 10.
  2. Step 2: Check reassignment

    Trying to assign 20 to a is illegal because constants cannot change value.
  3. Final Answer:

    Cannot assign a new value to a constant variable -> Option C
  4. Quick Check:

    Constants cannot be reassigned [OK]
Hint: Constants cannot be changed after initialization [OK]
Common Mistakes:
  • Trying to reassign constants
  • Ignoring compiler error messages
  • Confusing constants with variables
5. You want to define a constant literal for the value of pi (3.14159) that cannot be changed and can be used throughout your program. Which of the following is the best way to do this in C++?
hard
A. const double pi = 3.14159;
B. #define pi 3.14159
C. float pi = 3.14159;
D. double const pi;

Solution

  1. Step 1: Understand constant literal requirements

    The value should be fixed, unchangeable, and precise (double is better than float).
  2. 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.
  3. Final Answer:

    const double pi = 3.14159; -> Option A
  4. Quick Check:

    Use 'const double' with initialization for constants [OK]
Hint: Use 'const double' with value for precise constants [OK]
Common Mistakes:
  • Using non-const variables for constants
  • Using #define instead of const
  • Not initializing constants
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard