Learn how to define fixed values that cannot be changed during program execution.
In programming, a constant is a value that cannot be altered by the program during its normal execution. Using constants makes the code more readable and maintainable by giving a meaningful name to a fixed value. For instance, using `PI` instead of `3.14159` makes the code's intent clearer. C provides two primary ways to define constants. The first method is by using the `const` keyword. When you declare a variable with the `const` qualifier, you are telling the compiler that its value is read-only and must be initialized at the time of declaration. Any attempt to modify a `const` variable later in the code will result in a compilation error. This is the modern, preferred method as it is type-safe, meaning the compiler knows the data type of the constant and can perform checks accordingly. For example, `const double PI = 3.14159;`. The second method is by using the `#define` preprocessor directive. This directive is used to create macros. For example, `#define PI 3.14159`. Before the code is compiled, the preprocessor scans the file and replaces every occurrence of the identifier `PI` with the value `3.14159`. This is a simple text substitution and is not type-safe. While widely used in older C code, it can sometimes lead to unexpected behavior if not used carefully. Literals, on the other hand, are the actual fixed values themselves written in the source code, such as `101` (an integer literal), `3.14` (a floating-point literal), or `'A'` (a character literal). They are the raw data assigned to variables or constants.