What are Symbolic Constants in C? See Example

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:

  1. The #define preprocessor directive
  2. The const keyword

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

  • PI and MAX_STUDENTS are 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 PI to 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

  • const variables 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#defineconst
Processed byPreprocessorCompiler
Type checking❌ No✅ Yes
Memory allocation❌ No✅ Yes
ScopeGlobal onlyBlock or global
DebuggingHarderEasier
UsageGood for macros, global constantsGood 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

🔗 External C Documentation:

https://www.w3schools.com/c

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *