What is a constant expression

Daniel Elbaum dan at oresoft.uu.net
Fri Oct 20 05:22:03 AEST 1989


In article <1219 at crdos1.crd.ge.COM> davidsen at crdos1.UUCP (bill davidsen) writes:
:
:  Trying to get a net program working has brought up an interesting
:question as to what is a constant expression.

<the expression>:

:	#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.

Referring to the 12/88 dpANS:

It's listed under "constant" in the index, of course. :-)

Section 3.4 describes basically three kinds of constant expressions
that can be used in initializers: arithmetic constants, null pointer
constants, and address constants plus or minus integral constants.
Arithmetic constant expressions can only contain integral constant
operands, so they're out.  Address constants point to static storage
or to functions, and can contain [], ., ->, &, and *, but you're not
allowed to use the values of objects obtained by using them.

	I guess XJ311 didn't want to bother distinguishing between
constant-pointer-to-constant-object and constant-pointer-to-
arbitrary-object for compile-time rules.


-- 
Spa link snot the temper tent, a few cannery doubt lowed.

({uunet,tektronix,reed,sun!nosun,osu-cis,psu-cs}!oresoft!(dan)@oresoft.uu.net)



More information about the Comp.std.c mailing list