Constants in C are fixed values that do not change during the execution of a program. Once defined, a constant cannot be altered.
๐ Why Use Constants? They improve code readability, prevent accidental changes, and make maintenance easier.
const keyword: Declares a variable as constant.#define preprocessor: Defines a constant value globally.
#include <stdio.h>
#define PI 3.14 // Constant using #define
int main() {
const int MAX = 100; // Constant using const keyword
printf("PI is: %f\\n", PI);
printf("Max limit is: %d\\n", MAX);
return 0;
}
const is type-safe and preferred for variables.#define is a preprocessor directive and has no type.MAX_SPEED, PI, etc.
const declaration.const to store the maximum marks in a test.#define for the value of gravity (e.g., 9.8).Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!