What is the difference between a variable and a constant in C?
A variable stores a value which can change. A constant stores a value which cannot change after it is set.
#include <stdio.h>
#define DAYS 7
int main(void) {
int count = 5;
const float PI = 3.14f;
count = 10; // allowed
// PI = 3.15f; // error: constant cannot change
printf("%d %d %.2f\n", count, DAYS, PI);
return 0;
}
Output:
Output of the above program is
10 7 3.14
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |