Learn

In most cases, variable values can be changed in the program, however, there are cases where you do not want to allow for your variables to change value. This is where constants come into play. These special types prevent any changes during execution once the value is set at declaration.

Any basic data type in C, like those we have gone over, can be declared as a constant using the keyword const before the type. So instead of our template of type variable_name, it would be const type variable_name.

It is also a best practice to use all upper case letters when declaring a constant:

const int DAYSINWEEK = 7;

Instructions

1.

The speed of light is a constant, so a great candidate for an example of the const variable type. For the first step, identify the SPEEDOFLIGHT as a constant.

Notice that the name is already fully capitalized to match constant best practices.

2.

The program now violates the rules of constants. We know the speed of light won’t change, so how can you fix the code to run correctly by obeying the rules of constants?

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?