C TutorialLesson 10Free preview
C Constants
This lesson is part of C · All levels
Summary
A constant is a value that is set when a program is compiled and cannot be changed during the program's run. In C there are two main ways: declare the variable with the keyword const, or define a macro via #define that is a substitution of text before compilation. Constant is useful for fixed values such as taxes, unit conversions, or capacity to make the code easy to maintain and free of typing. If there is an attempt to change the value, the compiler will reject it.
Key points
- Const marks a variable whose value cannot be changed after initialisation
- #define makes a macro constant that is replaced by a preprocessor before compilation
- Changing the const value results in a compilation error
- Constant makes the program easier to maintain: one change is enough in one place
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.