DEV Community

Cover image for What is a Constant?
Mukesh Kumar
Mukesh Kumar

Posted on

What is a Constant?

🎙️ Introduction

Hello everyone!

In the previous chapters, we learned about variables — how they store data and how their values can change during program execution.

Now let me ask you something:

👉 What if we have a value that should never change?
👉 What if we want to protect a value from being modified...Read More

🔹 Step 1: Basic Meaning of a Constant

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

In simple words:

👉 A constant is a fixed value.

Unlike variables, whose values can change, constants rem...Read More

🔹 Step 2: Why Do We Need Constants?

Imagine writing a program where the value of π is used in many calculations.

If we write 3.14 again and again in different places, it becomes difficult to manage.

Now imagine if the value needs to be updated — we would...Read More

🔹 Step 3: Difference Between Variable and Constant

Let us clearly understand the difference.

Variable:

Value can change.

Used for dynamic data.

Example: age, marks, balance.

Constant:

Value cannot change.

Used for fixed data....Read More

🔹 Step 4: Types of Constants in C

In C language, constants are mainly of different types:

1. Numeric Constants

These include:

Integer constants → 10, 25, -100

Floating constants → 3.14, 0.5, -2.7

2. Character Constants

Single character enclosed in single quotes:

'A'

'b'

'5'

3. String Constants

Group of characters enclosed in double quotes...Read More

🔹 Step 5: Defining Constants in C

In C, we can define constants using:

1. const Keyword

This makes a variable constant.

Example concept:
We declare a variable with const, and once assigned, its...Read More

🔹 Step 6: Rules for Constants

Constant values cannot be modified after definition.

Must follow proper data type.

Should be written in uppercase by convention when using...Read More

🔹 Step 7: Real-Life Example

Imagine a school program where passing marks are fixed at 40.

Instead of writing 40 everywhere, we define:

PASS_MARK = 40

Now if passing criteria changes to 35, we only...Read More

🔹 Step 8: Importance of Constants in Programming

Constants are important because:

They protect important values.

They reduce errors.

They improve code readability.

They make programs easier to update...Read More

📌 Summary

In this chapter, we learned:

A constant is a fixed value.

It cannot change during execution.

It improves safety and clarity...Read More

Top comments (0)