In C programming, symbolic constants are meaningful names that represent fixed values. Instead of using numbers or strings directly in your code, you assign them a name — making your programs easier to read, maintain, and update.
Symbolic constants do not change during program execution. They are typically defined using:
- The
#definepreprocessor directive - The
constkeyword
Let’s explore both methods in detail.
1. Defining Symbolic Constants Using #define
The #define directive allows you to assign a name to a value. It is processed by the preprocessor before the actual compilation starts.
Syntax
#define CONSTANT_NAME value
Example
#include <stdio.h>
#define PI 3.14
#define MAX_STUDENTS 100
int main() {
printf("Value of PI: %.2f\n", PI);
printf("Maximum students allowed: %d\n", MAX_STUDENTS);
return 0;
}
Explanation
PIandMAX_STUDENTSare symbolic constants.- They do not occupy memory.
- The compiler replaces them everywhere in the code before execution.
Benefits of Using #define
- Great for values used frequently.
- Easy to update in one place (e.g., changing
PIto 3.14159). - Often used for configuration values, array sizes, limits, etc.
2. Defining Symbolic Constants Using const Keyword
The const keyword creates a variable whose value cannot be modified after initialization.
Syntax
const data_type variable_name = value;
Example
#include <stdio.h>
int main() {
const float GST_RATE = 0.18;
printf("GST Rate: %.2f\n", GST_RATE);
// GST_RATE = 0.20; // ❌ Error: assignment of read-only variable
return 0;
}
Key Points
constvariables do occupy memory, unlike#define.- They follow scope rules (block scope, local/global).
- They support type checking (unlike preprocessor constants).
Difference Between #define and const
| Feature | #define | const |
|---|---|---|
| Processed by | Preprocessor | Compiler |
| Type checking | ❌ No | ✅ Yes |
| Memory allocation | ❌ No | ✅ Yes |
| Scope | Global only | Block or global |
| Debugging | Harder | Easier |
| Usage | Good for macros, global constants | Good for typed constants |
3. Using Symbolic Constants for Better Code Readability
Example Scenario (Bad Coding Practice)
int bonus = 5000;
int total = salary + 5000;
Here, 5000 is a magic number. Using a symbolic constant improves clarity.
Using Symbolic Constant (Good Practice)
#define BONUS_AMOUNT 5000
int total = salary + BONUS_AMOUNT;
Now the code is:
- Meaningful
- Easy to update
- Less error-prone
4. Symbolic Constants in Real C Programs
Example: Defining Array Sizes
#define SIZE 50
int marks[SIZE];
Example: Using const in Functions
void printMessage(const char message[]) {
printf("%s", message);
}
5. Best Practices for Symbolic Constants
✔ Use uppercase names for constants:MAX_SPEED, PI, GST_RATE
✔ Avoid using magic numbers in code.
✔ Use const when type safety is important.
✔ Use #define for compile-time constants or macro values.
✔ Keep constant names descriptive.
Conclusion
Symbolic constants make C programs more readable, maintainable, and robust. Whether you use #define or const, the goal is the same — to avoid hard-coded values and make your program easier to understand and update.
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming