C Constants


Definition

When you need to lock in a value so that it can’t be changed—either accidentally or intentionally—you use the const modifier. It creates a read-only variable whose content stays fixed forever once assigned.


Illustration

const int speedLimit = 80;  // speedLimit is permanently set to 80 
SpeedLimit = 100;               // ❌ Error: speedLimit is immutable 

When to Use It

Apply const when the value is permanent or represents a universal truth within your program logic.

const int HOURS_IN_DAY = 24;

Important Rule

Every constant must be immediately initialized when declared. Delayed assignment is not allowed.

Valid:

const float PI = 3.14;

Invalid:

const float PI; 
PI = 3.14;    // ❌ Compile-time error 

Style Tip

By convention, constant identifiers are written using all capital letters to make them stand out and signal their unchangeable nature.

const int MAX_USERS = 100;

This isn’t required by the compiler, but it improves clarity for people reading your code.


Summary Points

  • const ensures that a variable cannot be changed after assignment.
  • Constants must be initialized at declaration.
  • It’s good style to use UPPERCASE names for constant variables.
  • Helps prevent logic errors and maintains integrity in your program.

Prefer Learning by Watching?

Watch these YouTube tutorials to understand C Tutorial visually:

What You'll Learn:
  • 📌 Constants in C (Part 1)
  • 📌 C_07 Constants in C | Types of Constants | Programming in C
Previous Next