What is a constant expression

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Thu Oct 19 01:49:52 AEST 1989


  Trying to get a net program working has brought up an interesting
question as to what is a constant expression.

  Background: large program, defines ASCII control characters with a
macro as follows:
	#define CNTL(c) ('c' & 037)
While this work fine with pcc, the ANSI preprocessor doesn't expand it.
I know that there is now a way to expand to a string, so I looked for
the way to expand to a character. I dind't find it. Therefore, rather
than plow through thousands of lines of someone else's code to change
all occurences, I redefined the macro as:
	#define CNTL(c) (#c[0] & 037)

  This expands as follows: CNTL(M) becomes ("M"[0] & 037). No problem,
the string is a constant, the subscript is a constant, the octal value
is a constant, and the compiler was happy. Except... where the macro was
used to initialize a static value, in which case it was "not a constant
expression."

  What am I missing in the standard? In looking up initializers in the
index, section 3.5.7 (L14) uses the term "constant expression." However
the index doesn't seem to list that term. I looked in the expressions
section and didn't find it. Obviously my assumption that it is an
expression made up entirely of elements whose value is known at compile
time" is incorrect.

  Don't waste your time telling me that's a bad way to do the macro, I
didn't write it, and I don't defend it.
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list