In C programming, both variables and constants are used to work with data. The key difference is whether the value associated with them can be modified during program execution.
A variable is a named storage location whose value can be changed during the execution of a program. Variables are useful when a program needs to store data that may change as the program runs.
For example:
int age = 20;
age = 21;
Here, age is a variable. Its initial value is 20,
but it can later be changed to 21.
A constant is a fixed value that is not intended to be
modified after it has been defined. In C, the const keyword
can be used to declare an object whose value cannot be modified through
that identifier.
const float PI = 3.14159f;
Here, PI is a const-qualified object. Attempting
to assign a new value to it is not allowed:
PI = 3.14f; // Invalid
| Variable | Constant |
|---|---|
| A variable represents a storage location whose value can be modified. |
A const-qualified object represents a storage
location whose value cannot be modified through that identifier.
|
| Its value can change during program execution. |
Its value remains fixed after initialization when accessed
through the const identifier.
|
It can be declared using data types such as
int, float, double, and
char.
|
The const qualifier can be applied to a declared
data type, such as const int or
const float.
|
Example: int x = 10;
|
Example: const int x = 10;
|
#include
int main()
{
int age = 20;
const float PI = 3.14159f;
age = 21;
printf("Age = %d\n", age);
printf("PI = %.5f\n", PI);
return 0;
}
In this example, age is a variable, so its value can be
changed from 20 to 21. In contrast,
PI is declared using const, so it cannot be
assigned a different value through that identifier.
Consider a program that calculates the area of a circle. The radius may change for different calculations, so it should be stored in a variable. The value of pi, however, is normally treated as a fixed value, so it can be represented using a constant.
float radius = 5.0f;
const float PI = 3.14159f;
float area = PI * radius * radius;
Here, radius can be changed whenever necessary, whereas
PI is intended to remain unchanged.
A common misconception is that a C constant always occupies ROM, while a variable always occupies RAM. This is not a reliable way to distinguish them. The actual storage and memory placement depend on the compiler, target system, storage duration, and other factors. The important distinction is whether the program is allowed to modify the object through its identifier.
Also, #define creates a preprocessor macro,
not a variable or a const-qualified object. For example:
#define MAX_MARKS 100
Here, MAX_MARKS is replaced by the preprocessor before the
compiler processes the program. It is therefore different from:
const int MAX_MARKS = 100;
The difference can be remembered with one simple rule:
Variable → Value can change.
Constant → Value is intended to remain fixed.
Therefore, use a variable when the value needs to change during program execution, and use a constant when a value should remain unchanged.