Example of something which is, and is not, a "Constant Expression" in C?

Dave picture Dave · Sep 20, 2010 · Viewed 14.2k times · Source

I'm a tad confused between what is and is not a Constant Expression in C, even after much Googleing. Could you provide an example of something which is, and which is not, a Constant Expression in C?

Answer

Carl Norum picture Carl Norum · Sep 20, 2010

A constant expression can be evaluated at compile time. That means it has no variables in it. For example:

5 + 7 / 3

is a constant expression. Something like:

5 + someNumber / 3

is not, assuming someNumber is a variable (ie, not itself a compile-time constant).