C++ Constants

In C++, a constant is a value that cannot be changed during the execution of the program. Constants are useful for defining values that should not be modified, such as mathematical constants or program-specific values that should not be altered.

Here is an example of how to declare a constant in C++:

const int MAX_VALUE = 100; // declare a constant integer named "MAX_VALUE" with the value 100

In this example, we declare a constant integer named "MAX_VALUE" with the value 100. The "const" keyword specifies that this variable is a constant and cannot be modified.

You can also declare and initialize multiple constants in a single statement:

const int MAX_VALUE = 100, MIN_VALUE = 0; // declare two constant integers

In this example, we declare two constant integers named "MAX_VALUE" and "MIN_VALUE" with values 100 and 0, respectively.

C++ also allows you to define constants of other types such as floating-point constants or character constants:

const double PI = 3.14159; // declare a constant double named "PI" with the value 3.14159
const char NEWLINE = '\n'; // declare a constant character named "NEWLINE" with the value '\n'

In these examples, we declare a constant double named "PI" with the value 3.14159, and a constant character named "NEWLINE" with the value '\n'.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

79314