1. Introduction
A constant in C is a value that cannot be changed during program execution. Constants are used to represent fixed values like mathematical constants (e.g., PI), configuration limits, or flags, making code more readable, maintainable, and less error-prone than scattering 'magic numbers' throughout a program.
Cricket analogy: The LBW rule's height of stumps never changes mid-match, just as PI stays fixed in a program instead of being scattered as unexplained magic numbers across the scorecard code.
C offers several ways to define constants: literal constants embedded directly in expressions, the preprocessor #define directive, the const type qualifier, and enumeration constants declared with enum.
Cricket analogy: Just as a match can be defined by literal overs (20 for T20), a printed rulebook clause (#define), an umpire's fixed decision (const), or named categories like 'powerplay' (enum), C offers several ways to fix a value.
2. Syntax
#define CONSTANT_NAME value
const dataType variableName = value;
enum { CONST1, CONST2, CONST3 };3. Explanation
#define is a preprocessor directive that performs a simple textual substitution before compilation begins — it does not create a real variable, has no type, and consumes no memory of its own; the preprocessor just replaces every occurrence of the name with its value. The const qualifier, by contrast, creates an actual typed variable whose value cannot be modified after initialization; it is type-checked by the compiler and obeys normal scope rules, making it generally safer and preferred in modern C.
Cricket analogy: #define is like the groundstaff painting a boundary rope marker before the match starts (text substitution beforehand, no lasting entity), while const is like a permanent stadium fence that's inspected and enforced by officials for the whole game.
Literal constants include integer constants (42, 0x2A, 052), floating-point constants (3.14, 2.5e3), character constants ('A'), and string literals ("hello"). Suffixes clarify literal types: 'L'/'l' for long, 'U'/'u' for unsigned, 'F'/'f' for float (e.g., 100UL, 3.14f). A const variable must be initialized at the point of declaration since it cannot be assigned afterward.
Cricket analogy: Denoting a score as 250runs vs 250balls is like C's suffixes: '100UL' tags a value as unsigned long the way a scorecard tags '250*' as not-out, and a const total must be set the moment the innings closes, not adjusted afterward.
Tip: Prefer 'const' or 'enum' over '#define' for typed constants in modern C — they integrate with the type system, respect scope, and are visible to the debugger, whereas #define macros are invisible after preprocessing.
4. Example
#include <stdio.h>
#define PI 3.14159
int main(void) {
const int MAX_USERS = 100;
const float TAX_RATE = 0.18f;
int radius = 5;
float area;
area = PI * radius * radius;
printf("PI = %.5f\n", PI);
printf("Circle area = %.2f\n", area);
printf("MAX_USERS = %d\n", MAX_USERS);
printf("TAX_RATE = %.2f\n", TAX_RATE);
/* MAX_USERS = 200; would cause a compile-time error */
return 0;
}5. Output
PI = 3.14159
Circle area = 78.54
MAX_USERS = 100
TAX_RATE = 0.186. Key Takeaways
- Constants hold fixed values that cannot change after being set.
- #define performs textual substitution at preprocessing time and has no type.
- const creates a typed, scoped variable that is checked by the compiler.
- A const variable must be initialized at declaration and cannot be reassigned later.
- Literal suffixes (U, L, F) specify the exact type of numeric literals.
- enum constants provide a readable way to define related sets of named integer constants.
Practice what you learned
1. What is the key difference between a #define macro constant and a const variable?
2. Which of these is a valid way to declare an integer constant equal to 10 using const?
3. What does the suffix 'UL' indicate on the literal 100UL?
4. Which statement about #define is TRUE?
5. What happens if you attempt to modify a const variable after initialization, e.g. const int x = 5; x = 10;?
Was this page helpful?
You May Also Like
Variables in C
Learn C variables: declaration, initialization, naming rules, scope, and exam-ready examples with compilable code.
Data Types in C
Master C data types — int, float, char, double, and modifiers — with sizes, ranges, format specifiers, and examples.
Enumerations (enum) in C
Learn C enumerations: syntax, default integer values, custom value assignment, and best practices with examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics