LearnContact
Lesson 107 min read

Constants

Not every value in a program should change. In this lesson, you will learn what constants are, how they differ from variables, and how to define them correctly using the const keyword and #define directive.

Introduction

In the previous lesson, you learned that variables store values that can change during the execution of a program. However, not every value in a program should change. For example, the number of days in a week is always 7, and the value of π (Pi) remains approximately 3.14159. These values remain the same throughout the program. Such values are called Constants.

What is a Constant?

A constant is a fixed value whose value cannot be changed during the execution of a program. Once a constant is defined, it remains the same until the program terminates. Unlike variables, constants are read-only.

Real-World Analogy

Imagine a country's national flag. The flag represents the country and remains the same every day. It cannot be changed by ordinary citizens. Similarly, a constant stores a value that remains unchanged throughout the program.

Why Do We Need Constants?

Instead of writing the same fixed value repeatedly, we define it once and use its name throughout the program. Constants make programs:

More Readable

Meaningful names explain what the value represents.

Easier to Maintain

Update a value in one place instead of everywhere.

Less Prone to Errors

Prevents accidental modification of critical values.

More Meaningful

Code becomes self-explanatory.

Variable vs Constant

FeatureVariableConstant
ValueValue can changeValue cannot change
AccessRead and writeRead-only
UsageUsed for changing dataUsed for fixed data
MemoryMemory value can be modifiedMemory value remains fixed

Types of Constants

C supports different kinds of constants. The most common types are:

Integer Constants

Whole numbers without a decimal point.

  • 10, 25, 100
  • 5000, -45

Floating-Point

Contain decimal values for precision.

  • 3.14, 25.75
  • 99.99, -12.5

Character Constants

Single character in single quotes.

  • 'A', 'B'
  • '7', '#'

String Constants

Multiple characters in double quotes.

  • "Hello"
  • "India"

Enumeration

Named constants using the enum keyword.

  • MONDAY
  • TUESDAY

Ways to Define Constants

In C, constants can be defined in multiple ways. The two most common methods are:

1. Using the const Keyword

The const keyword creates a variable whose value cannot be modified.

c
const float PI = 3.14159;

// const makes it read-only
// float is the data type
// PI is the constant name

2. Using the #define Directive

The preprocessor replaces every occurrence of the name with the value before compilation.

c
#define PI 3.14159

// No semicolon at the end!
// Replaces PI with 3.14159

Memory Representation

Suppose we define PI = 3.14159. The memory representation looks like this:

Memory View
Constant Name: PI
       │
       ▼
+----------------+
|   3.14159      |
+----------------+
    (Read Only)

// Unlike variables, this value cannot be modified.

Real-World Examples

Many real-world values never change. These are excellent candidates for constants:

  • Days in a Week = 7
  • Months in a Year = 12
  • Hours in a Day = 24
  • Maximum Marks = 100
  • GST Rate = 18
  • Speed of Light = 299792458

Advantages of Constants

Improved Readability

Instead of writing 3.14159 everywhere, you can use PI. The program becomes easier to understand.

Easy Maintenance

Suppose the tax rate changes. Instead of updating every occurrence, you only update the constant definition once.

Prevents Accidental Changes

Since constants cannot be modified, they protect important values from accidental changes during execution.

Better Code Quality

Meaningful constant names make programs self-explanatory and professional.

Naming Conventions

Programmers usually write constant names using uppercase letters with underscores separating words. This makes constants easy to identify.

Examples
PI
MAX_SIZE
TAX_RATE
MAX_MARKS
BUFFER_SIZE

Variable vs Constant Example

Suppose a school management system stores Student Name, Age, and Marks. These values change, so they are variables. However, Maximum Marks = 100 never changes, so it should be stored as a constant.

c
#include <stdio.h>

#define MAX_MARKS 100  // Constant

int main() {
    int studentMarks = 85; // Variable
    
    printf("Marks: %d / %d\n", studentMarks, MAX_MARKS);
    
    // MAX_MARKS = 200; // ❌ Error! Cannot modify constant
    
    return 0;
}

Common Beginner Mistakes

Trying to Modify a Constant
// Incorrect:
PI = 3.15; // Error!

// Correct:
const float PI = 3.14159; // Define it once
Using Poor Constant Names
// Poor:
#define a 3.14

// Better:
#define PI 3.14159
Confusing Variables with Constants

Incorrect: Using a variable for a value that never changes. Better: Use a constant whenever the value should remain fixed.

Writing Constant Names in Mixed Case
// Less readable:
const float Pi = 3.14;
const float TaxRate = 0.18;

// Better:
const float PI = 3.14;
const float TAX_RATE = 0.18;

Best Practices

  • Use constants for fixed values.
  • Choose meaningful names that describe the value.
  • Write constant names in uppercase, such as MAX_SIZE.
  • Avoid repeating fixed values throughout the program.
  • Use const whenever appropriate for type safety.

Frequently Asked Questions

What is a constant?

A constant is a fixed value that cannot be changed during program execution.

How is a constant different from a variable?

A variable can change its value. A constant always retains the same value.

Can constants be modified?

No. Once defined, a constant cannot be modified.

Why should I use constants?

Constants improve readability, reduce errors, and make programs easier to maintain.

What are some examples of constants?

Examples include PI, MAX_SIZE, DAYS_IN_WEEK, MONTHS_IN_YEAR, and TAX_RATE.

Key Takeaways

  • Constants store fixed values.
  • Their values cannot change during execution.
  • They improve program readability and maintenance.
  • Constants can be defined using the const keyword or the #define directive.
  • Use meaningful uppercase names for constants.

Summary

Constants are an essential part of C programming because they represent values that remain unchanged throughout a program. By replacing repeated fixed values with meaningful names, constants make programs easier to read, maintain, and debug. Understanding the difference between variables and constants helps you write cleaner and more reliable code.

Next Lesson →

Data Types