C Constants

As we discussed before, variables hold different values, but in some cases, we need to declare a variable as read-only and unchangeable. To do this, we are required to use the const keyword before the variable type. Look at the following example to understand C constants:

const int temp = 18
// temp will always be 18 and cannot be changed

If I try to change temp, I would get an error:

temp = 25;
//error: assignment of read-only variable 'temp'

Hands-on practice: try this on your computer and see the result.

Important points on C constants

  1. Const is used for variables that their values are very unlikely to change.
  2. When declaring a const, it must be assigned a value:
const int temp;
temp = 15; //error
Was this helpful?
[0]
Scroll to Top
Scroll to Top