const int DAYS_IN_WEEK = 7;
DAYS_IN_WEEK = 8;
// ❌ COMPILER ERROR — constants refuse to changeConstants in C: Fixed Values, Declaration Methods, and Examples
What is Constants in C? Core Concept & Direct Summary
What are the different ways to define constants in C using `const`, `#define`, and `enum`?
Some values should never change. The number of days in a week. The value of π. The maximum score on a test. The speed of light.
Now imagine writing a program where someone — future-you, a teammate, a tired intern — accidentally typesPI = 4.0somewhere in the middle of 5,000 lines. Your geometry calculations silently go insane, and nobody knows why.
C's answer to this problem is theconstant: a value locked in place, immune to modification. In this guide, you'll learn the two ways to create constants in C (constand#define), the differentkindsof constant values C recognizes, and the practical judgment about which method to use when.
Let's lock some values down.
How Does Constants in C Work? In-Depth Mechanics & Architecture
What Is a Constant in C?
Aconstantis a value that cannot be modified after it's defined. Where a variable is a box you can empty and refill, a constant is a box that's beenwelded shut.
That compiler error is the entire point. The mistake gets caughtbeforethe program ever runs — not at 2 AM in production when a customer calls.
Constants deliver three benefits:
- Safety— accidental modification becomes impossible
- Readability—
DAYS_IN_WEEKexplains itself; a bare7doesn't - Maintainability— change the value inone place, and it updates everywhere it's used
That last benefit is bigger than it sounds. If a tax rate is hard-coded as0.18in 47 places across your program, changing the rate means 47 edits and 47 chances to miss one. As a constant? One edit. Done.
Method 1: The const Keyword (The Modern Way)
Theconstqualifier turns any variable declaration into a locked constant:
Key facts aboutconst:
- It's type-checked— the compiler knows
PIis afloat, so format specifiers, conversions, and warnings all work normally - It must be initialized at declaration—
const int x;followed byx = 5;is illegal; once declared, the value is frozen - It has scope— a
constdeclared inside a function is local to that function, just like a variable
Try modifying one and see the compiler's disapproval firsthand:
The compiler literally calls itread-only— a perfect description.
Method 2: The #define Directive (The Classic Way)
Beforeconstexisted, C used thepreprocessorfor constants — and this method is still everywhere:
Here's the crucial difference inhowit works:#defineperforms atext replacementbefore compilation. The preprocessor literally finds everyPIin your code and swaps in3.14159— the compiler never sees the name at all. It's a search-and-replace operation, not a variable.
Notice the style details:
- No semicolonat the end (it's a preprocessor directive, not a statement)
- No
=sign— just the name and the replacement text - ALL_CAPS naming— the universal convention that screams "I'm a constant!"
const vs #define: Which Should You Use?
This is the question every C learner asks, so here's the honest comparison:
The modern consensus:preferconstfor values — it's safer, scoped, and debuggable. Use#definefor thingsconstcan't do (array sizes in older standards, conditional compilation, macros with logic).
[IMAGE 2 — Infographic: const vs #define Showdown]Design:VS-battle style card — left side
const(green checkmarks: typed, scoped, debuggable), right side#define(blue checkmarks: works for array sizes, macros). Verdict banner: "Prefer const for values."Alt text:Difference between const keyword and define directive in C comparison table
The Literal Constants: Fixed Values You Write Directly
Beyond named constants, C recognizesliteral constants— fixed values typed straight into code. You've been using them all along:
Integer Constants
Yes,42,052,0x2A, and0b101010are thesame numberwearing different outfits. The leading0for octal is a famous trap — writeint pincode = 042;and you won't get 42.
Floating-Point Constants
Beginner trap:without thef,3.14is adouble, and storing it in afloatsilently demotes the precision. Old compilers warn; new habits don't need warnings.
Character Constants
Remember the golden rule from our tokens discussion:'A'(character) and"A"(string) aredifferent thingsin C. Single quotes = one character. Double quotes = a string.
String Constants
String literals get stored in read-only memory — which means this classic mistake crashes programs:
A Complete Working Example
All constant styles cooperating in one program:
Output:
Notice how the business logic reads almost like English —subtotal * TAX_RATE— because the constants carry meaning. And if the tax rate changes to 20%? One line edited, entire program updated.
Best Practices at a Glance
- Name constants in ALL_CAPS—
MAX_SIZE,PI,TAX_RATE - Prefer
constfor typed values; use#definefor array sizes and macros - Never hard-code "magic numbers"— if a value has meaning, name it
- Define once, use everywhere— that's the entire maintenance superpower
- Don't modify string literals— they're read-only, and C won't warn you politely
Runnable C Code Example & Practical Implementation
#include <stdio.h>
#define APP_VERSION "2.5.0"
const int MAX_USERS = 500;
enum Status {
SUCCESS = 0,
WARNING = 1,
ERROR = 2
};
int main() {
printf("Version: %s\
", APP_VERSION);
printf("Max Users: %d\
", MAX_USERS);
printf("Status Code Error: %d\
", ERROR);
return 0;
}Key Takeaways, Best Practices & Memory Safety Rules
const variables and enum over #define for typed numeric constants to benefit from compiler type safety.Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked C programming interview questionsQ: What is the main advantage of `const` over `#define`?
Q: Can you modify a `const` variable via a pointer in C?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online C Compiler & Runner — Interactive online GCC sandbox to execute C code in real time.
- Online C Code Editor & IDE — Multi-file C programming environment with code formatting.
- C++ Programming Tutorial — Transition from procedural C to Object-Oriented C++ with STL.
- JavaScript Programming Tutorial — Master modern JavaScript fundamentals, DOM APIs, and async patterns.
Authoritative Standards & Citations
- ISO/IEC 9899 (C Language Standard) — Official international C specification by ISO/IEC JTC 1/SC 22/WG 14.
- GNU GCC Documentation — Official reference manual for the GNU Compiler Collection and C libraries.
- The C Programming Language (K&R) — Definitive classic work by Brian Kernighan & Dennis Ritchie at Bell Labs.
- C Reference Documentation (cppreference.com) — Verified reference covering C89 through C23 language features.