In C programming, constants are fixed values that cannot be altered during program execution. They are typically used to represent values that remain constant throughout the program. Constants can be categorized into two types:
Literal Constants: These are fixed values like numbers or characters.
Symbolic Constants: These are identifiers that represent constant values.
Here's how you can define and use constants in C:
Integer Constants:
Floating-point Constants:
Character Constants:
Symbolic constants are defined using the #define
directive or the const
keyword.
#define
Directive:const
Keyword:In this example:
MAX_VALUE
is a symbolic constant defined using #define
.MIN_VALUE
is a symbolic constant defined using const
.Constants are useful for making code more readable, maintainable, and less error-prone. They provide a way to give meaningful names to values used in the program and make it easier to update these values if needed.