Constants and literals - Time & Space Complexity
When we use constants and literals in C code, we want to know how they affect the program's speed.
We ask: Does using constants or fixed values change how long the program takes as input grows?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
const int x = 5;
int y = 10;
int sum = x + y;
printf("Sum is %d\n", sum);
return 0;
}
This code adds two numbers where one is a constant and prints the result.
Look for loops or repeated steps that happen many times.
- Primary operation: Simple addition and print statement.
- How many times: Runs only once, no loops or repetition.
Since the code does not repeat or loop, the time it takes stays the same no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 operations |
| 100 | About 3 operations |
| 1000 | About 3 operations |
Pattern observation: The number of steps does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "Using constants makes the program faster as input grows."
[OK] Correct: Constants do not change speed with input size because they are fixed values used once, not repeated operations.
Understanding that constants and literals do not affect time growth helps you focus on parts of code that really change with input size.
"What if we replaced the constant with a loop that adds numbers repeatedly? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of constant
A constant is a value that remains the same throughout the program and cannot be modified.Step 2: Compare options with definition
Only A fixed value that cannot be changed during program execution describes a fixed value that does not change, which matches the definition of a constant.Final Answer:
A fixed value that cannot be changed during program execution -> Option BQuick Check:
Constant = fixed value [OK]
- Confusing constants with variables
- Thinking constants are functions
- Mixing constants with loops
Solution
Step 1: Review ways to define constants in C
Constants can be defined using#defineor usingconstkeyword with variable declaration.Step 2: Check each option
#define MAX 100 uses#define, which is valid. int const MAX = 100; usesint const, which is valid. const int MAX = 100; usesconst int, also valid. So all are correct.Final Answer:
All of the above -> Option AQuick Check:
All methods define constants [OK]
- Using variable without const
- Missing semicolon in const declaration
- Confusing #define with variable
#include <stdio.h>
#define PI 3.14
int main() {
const int radius = 5;
float area = PI * radius * radius;
printf("%.2f", area);
return 0;
}Solution
Step 1: Calculate area using given formula
Area = PI * radius * radius = 3.14 * 5 * 5 = 3.14 * 25 = 78.5Step 2: Check printf formatting
Using %.2f prints the float with 2 decimal places, so output is 78.50Final Answer:
78.50 -> Option CQuick Check:
Area = 3.14 * 25 = 78.50 [OK]
- Using wrong formula
- Ignoring decimal places
- Confusing radius with diameter
const int x;
x = 10;
printf("%d", x);Solution
Step 1: Understand const variable rules
A const variable must be initialized when declared and cannot be assigned a value later.Step 2: Analyze the code
Here,const int x;declares x without initialization, then tries to assign 10 later, which is not allowed.Final Answer:
Cannot assign value to const variable after declaration -> Option AQuick Check:
const variables must be initialized once [OK]
- Assigning value after declaration
- Not initializing const variables
- Ignoring const rules
Solution
Step 1: Understand const array declaration
Declaring an array withconst int arr[3]means the elements cannot be changed after initialization.Step 2: Check each option
int arr[3] = {1, 2, 3}; declares a normal array, elements can be changed. const int arr[3] = {1, 2, 3}; declares a constant array correctly. #define arr {1, 2, 3} uses #define incorrectly for array. int const *arr = {1, 2, 3}; declares a pointer to const int, but initialization is invalid.Final Answer:
const int arr[3] = {1, 2, 3}; -> Option DQuick Check:
const array elements cannot change [OK]
- Using #define for arrays
- Declaring non-const arrays
- Incorrect pointer initialization
Ready to prove it?
Pass the test and this pattern moves unknown
