Constant or Literal


#1

In C a constant is defined as a value that cannot be changed during execution. However, a variable is declared and initiated as:
int count = 0;

A literal constant is declared as:
int count = 20;

What the heck am I missing here?


#2

A variable is declared as:

int count;

At your option you may also set the initial value as in:

int count = 0;

A constant may only be set once at declaration as in:

const int count = 20;

This locks count at 20, never to be changed.

Mitch


#3

Thanks mitchb,

You’re right, of course, but every book I see on C declares a literal constant with the “int count = 5;” form. Without the const keyword. They later describe const almost as if it were optional. I will always use const when I mean it, just be make sure.