Constants and literals let you use fixed values in your program that do not change. This helps keep your code clear and safe.
Constants and literals
#define NAME value const type NAME = value;
#define creates a constant by replacing text before compiling.
const creates a typed constant variable that cannot be changed.
#define.#define DAYS_IN_WEEK 7daysInWeek with the value 7 using const.const int daysInWeek = 7;
letter with the value 'A'.const char letter = 'A';
pi with the value 3.14.const float pi = 3.14f;
This program shows how to use #define and const to create constants. It prints the maximum and passing scores.
#include <stdio.h> #define MAX_SCORE 100 int main() { const int passingScore = 60; printf("Maximum score is %d\n", MAX_SCORE); printf("Passing score is %d\n", passingScore); return 0; }
Constants created with #define have no type and are replaced before the program runs.
Constants created with const have a type and are checked by the compiler.
Use const when you want type safety and #define for simple text replacement.
Constants hold fixed values that do not change during program execution.
#define and const are two ways to create constants in C.
Using constants makes your code easier to read and less error-prone.
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
