const Variables
Make values read-only.
What const Means
The const keyword tells the compiler a variable's value must not change after initialization.
If you try to modify it later, compilation fails. This protects values that should stay fixed.
const int MAX_USERS = 100;Declaring a const
You must give a const variable its value when you declare it, because you cannot assign to it afterward.
Here PI is initialized once and used like any normal variable for reading.
#include <stdio.h>
int main(void) {
const double PI = 3.14159;
printf("%f\n", PI);
return 0;
}All lessons in this course
- Defining Enums
- Enum Values and Ranges
- const Variables
- #define Constants