C const Keyword
Example
Create a constant variable:
const int myNum = 15; // myNum will always be 15
// myNum = 10; // Error, because myNum is const
printf("%d", myNum);
Definition and Usage
The const keyword makes a variable constant, which means unchangeable and read-only. If you try to change it, the compiler gives an error.
Give a const variable its value when you create it, because you cannot change it later. Use it for values that should never change, like const int minutesPerHour = 60;.
You can also use const on a function parameter. Then the function cannot change that parameter.
Related Pages
Read more about constants in our C Constants Tutorial.