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.
What is a Constant in C?
A constant is a fixed value whose value does not change while the program is running.
Types of Constants in C
C constants are mainly divided into:
Numeric Constants
Character Constants
String Constants
Symbolic Constants
Enumeration Constants
Numeric Constants
Integer Constants
Floating-Point Constants
Example
Character Constants
A single character enclosed in single quotes.
Example:
- Stored internally as ASCII value
String Constants
A sequence of characters enclosed in double quotes.
Example:
- Automatically ends with
'\0'
Symbolic Constants using #define
Defined using preprocessor directive.
Syntax
Example
- No memory allocation
- No type checking
Constants using const Keyword (Recommended)
Syntax
Example
- Type safe
- Preferred over
#define
Difference: #define vs const
| Feature | #define | const |
|---|---|---|
| Type checking | No | Yes |
| Scope | Global | Block / Global |
| Debugging | Hard | Easy |
| Memory | No | Yes |
| Recommended | Less | More |
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
Common Mistakes
- Trying to modify a constant
- Confusing
const int *pwithint *const p Overusing#defineForgetting string'\0'
Interview Questions (Must Prepare)
What is a constant in C?
Difference between
#define andconstWhat are symbolic constants?
What is
enum?Explain
const with pointerAre 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-basedenum for grouped constants- Very important for clean code & interviews
