C Constants
🔒 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.
2️⃣ Types of Constants in C ⭐
C constants are mainly divided into:
-
Numeric Constants
-
Character Constants
-
String Constants
-
Symbolic Constants
-
Enumeration Constants
3️⃣ Numeric Constants 🔢
🔹 Integer Constants
🔹 Floating-Point Constants
Example
4️⃣ Character Constants 🔤
A single character enclosed in single quotes.
Example:
📌 Stored internally as ASCII value
5️⃣ String Constants 🧵
A sequence of characters enclosed in double quotes.
Example:
✔ Automatically ends with '\0'
6️⃣ Symbolic Constants using #define ⭐
Defined using preprocessor directive.
Syntax
Example
✔ No memory allocation
❌ No type checking
7️⃣ Constants using const Keyword ⭐ (Recommended)
Syntax
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.
Values:
✔ 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)
-
What is a constant in C?
-
Difference between
#defineandconst -
What are symbolic constants?
-
What is
enum? -
Explain
constwith pointer -
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
