Mastering C++: Part 2: Constants

Learning C++

Using C++

Mastering C++


Constants

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 Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.