C Constants

C Tutorial

🔒 C Constants (Complete Guide: Beginner → Advanced)

In C language, a constant is a value that cannot be changed during program execution.
Constants improve code safety, readability, and maintainability, and they are very important for interviews.


1️⃣ What is a Constant in C?

A constant is a fixed value whose value does not change while the program is running.

10, 3.14, 'A', "Hello"

2️⃣ Types of Constants in C ⭐

C constants are mainly divided into:

  1. Numeric Constants

  2. Character Constants

  3. String Constants

  4. Symbolic Constants

  5. Enumeration Constants


3️⃣ Numeric Constants 🔢

🔹 Integer Constants

10, -5, 100

🔹 Floating-Point Constants

3.14, -0.5, 2.0

Example

int a = 10;
float pi = 3.14;

4️⃣ Character Constants 🔤

A single character enclosed in single quotes.

'A', '9', '$'

Example:

char grade = 'A';

📌 Stored internally as ASCII value


5️⃣ String Constants 🧵

A sequence of characters enclosed in double quotes.

"Hello"
"Welcome to C"

Example:

char name[] = "C Language";

✔ Automatically ends with '\0'


6️⃣ Symbolic Constants using #define

Defined using preprocessor directive.

Syntax

#define PI 3.14159

Example


 

✔ No memory allocation
❌ No type checking


7️⃣ Constants using const Keyword ⭐ (Recommended)

Syntax

const data_type variable = value;

Example


 

✔ Type safe
✔ Preferred over #define


8️⃣ Difference: #define vs const

Feature #define const
Type checking ❌ No ✅ Yes
Scope Global Block / Global
Debugging Hard Easy
Memory No Yes
Recommended ❌ Less ✅ More

9️⃣ Enumeration Constants (enum) 🔥

Used for named integer constants.

enum Day {SUN, MON, TUE, WED};

enum Day today = MON;

Values:

SUN = 0, MON = 1, TUE = 2, WED = 3

✔ Improves readability
✔ Used in real projects


🔟 const with Pointers (Interview Favorite ⭐)


 

📌 Very important for advanced interviews


1️⃣1️⃣ Common Mistakes ❌

❌ Trying to modify a constant
❌ Confusing const int *p with int *const p
❌ Overusing #define
❌ Forgetting string '\0'


📌 Interview Questions (Must Prepare)

  1. What is a constant in C?

  2. Difference between #define and const

  3. What are symbolic constants?

  4. What is enum?

  5. Explain const with pointer

  6. Are constants stored in memory?


🔥 Real-Life Use Cases

  • Mathematical values (PI, GRAVITY)

  • Configuration values

  • Array size limits

  • Status codes

  • Embedded systems


✅ Summary

✔ Constants store fixed values
✔ Use const for type safety
#define is preprocessor-based
enum for grouped constants
✔ Very important for clean code & interviews

You may also like...