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.
#define or using const keyword with variable declaration.#define, which is valid. int const MAX = 100; uses int const, which is valid. const int MAX = 100; uses const int, also valid. So all are correct.#include <stdio.h>
#define PI 3.14
int main() {
const int radius = 5;
float area = PI * radius * radius;
printf("%.2f", area);
return 0;
}const int x;
x = 10;
printf("%d", x);const int x; declares x without initialization, then tries to assign 10 later, which is not allowed.const int arr[3] means the elements cannot be changed after initialization.Pass the test and this pattern moves unknown