Concept Flow - Constants and literals
Start
Write literal value
Assign to constant
Use constant in code
Constant value remains unchanged
End
This flow shows how a literal value is assigned to a constant and used without changing its value.
const int DAYS_IN_WEEK = 7; int main() { int totalDays = DAYS_IN_WEEK * 4; return totalDays; }
| Step | Action | Variable/Constant | Value | Note |
|---|---|---|---|---|
| 1 | Declare constant DAYS_IN_WEEK | DAYS_IN_WEEK | 7 | Constant assigned literal 7 |
| 2 | Enter main function | - | - | Program starts main |
| 3 | Calculate totalDays = DAYS_IN_WEEK * 4 | totalDays | 28 | 28 = 7 * 4 |
| 4 | Return totalDays | return | 28 | Program returns 28 |
| 5 | End | - | - | Program ends |
| Variable/Constant | Start | After Step 3 | Final |
|---|---|---|---|
| DAYS_IN_WEEK | undefined | 7 | 7 |
| totalDays | undefined | 28 | 28 |
Constants hold fixed values that cannot change. Literals are actual fixed values in code. Use 'const' keyword to declare constants. Constants improve code safety and readability. Example: const int DAYS = 7; DAYS cannot be changed later.