Article View: comp.std.c
Article #726What is a constant expression
From: Wm E Davidsen Jr
Date: Wed, 18 Oct 1989 15:49
Date: Wed, 18 Oct 1989 15:49
34 lines
2103 bytes
2103 bytes
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@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
Technical Details
Message-ID:
<1219@crdos1.crd.ge.COM>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!ginosko!uunet!crdgw1!crdos1!davidsenShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!ginosko!uunet!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.std.c,comp.lang.c Subject: What is a constant expression Message-ID: <1219@crdos1.crd.ge.COM> Date: 18 Oct 89 15:49:52 GMT Reply-To: davidsen@crdos1.UUCP (bill davidsen) Followup-To: comp.std.c Organization: GE Corp R&D Center Lines: 34