Constants in C
In C, constants are fixed values that don’t change during program execution — perfect for storing things like PI, max sizes, or fixed config values. ??
? Types of Constants in C
? 1. Literal Constants
These are actual fixed values written directly in code:
float pi = 3.14; // Float constantchar grade = 'A'; // Character constant
? 2. #define Constants (Macro)
Use the preprocessor to define constants:
#define PI 3.14159const int maxScore = 100;const float taxRate = 0.18;
Cannot be changed later in code
Has type checking at compile time
maxScore = 200; // ? Error: assignment of read-only variable
? Difference Between #define and const
| Feature | #define | const |
|---|---|---|
| Type-safe | ? No | ? Yes |
| Debuggable | ? Harder | ? Easier |
| Scope | Global only | Follows C scoping rules |
| Memory usage | No memory | May use memory |
? Constants with Arrays
const int SIZE = 5;int numbers[SIZE]; // Valid
? Example with Constants
#include #define PI 3.14int main() { const int radius = 5; float area = PI * radius * radius; printf("Area: %.2f\n", area); return 0;}