What if changing one number could fix your whole program instantly?
Why Constants and literals? - Purpose & Use Cases
Imagine you are writing a program that calculates the area of a circle multiple times using the value of pi. You type the number 3.14159 everywhere you need it.
Typing the same number repeatedly is slow and easy to mess up. If you want to change pi to a more precise value, you must find and replace every instance, risking mistakes and inconsistencies.
Constants let you name fixed values once. You write the value of pi just once as a constant, then use its name everywhere. This makes your code easier to read, change, and less error-prone.
area = 3.14159 * radius * radius;const double PI = 3.14159;
area = PI * radius * radius;Constants make your code clearer and safer by giving fixed values meaningful names you can reuse and update easily.
Think of a recipe that uses a fixed oven temperature. Instead of writing 350°F everywhere, you write a constant named OVEN_TEMP = 350, so if you want to bake at 375°F next time, you change it once.
Typing fixed values repeatedly is error-prone and hard to maintain.
Constants let you name and reuse fixed values safely.
This makes your code easier to read, update, and less buggy.
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
