Mastering C++: Part 2: Constants

Learning C++

Using C++

Mastering C++


In C, the proceeding programming language to C++, the keyword const was very common. A constant, in programming terminology, is anything that doesn’t change. It is paired with other variable types to keep them for being changed during the runtime of the program.

Hardware and Other Special Values

C++ is a system programming language. This means it can be used to write programs that work with hardware and other low-level functionality on different devices. Frequently, hardware have special needs and values to work with certain parts. Often, constants are used for this purpose.

Constants can also be used for other special values. While defining PI is useful example, other special values that do not change during runtime can also be defined as constants.

Static Constants

The keywords static and const can be combined together to create a shared value that does not change. Like other static entries, it would be shared across all objects. Through applying the const keyword, it also cannot be changed at any point while running.

Play with the example on!