On Fri, 05 Oct 2007 02:27:41 +0200, talkaboutqualit y
<t.******@ieee. orgwrote in comp.lang.c:
Need to define a macro as, say,
Why?
#ifdef NEED_FUNCTION
foo(x)
This is not a macro. Do you mean:
#define FOO(x) foo(x)
....???
>
#else
No need for an empty #else, eliminate it.
#endif
Say that macro is called FOO(x) and is used by writing
FOO(x);
with a semicolon after it so it looks like any other statement.
When NEED_FUNCTION is defined, then compiler sees
foo(x);
When NEED_FUNCTION is not defined, compiler sees
;
If I want to place
FOO(x);
in a larger function _before_ the local variable declarations, will I get
a compiler error (for doing something I shouldn't before declarations)?
Depends on the compiler and version of the C standard it conforms to.
What happened when you tried it?
If so, why?
If you do, it is because doing so is a constraint violation according
to the version of the C standard your compiler is conforming to.
If not, why not?
If you don't, it is because doing so is a legal practice according to
the version of the C standard your compiler is conforming to.
If I will get a compiler error, what can I define that macro as in the
else section so that that text, followed by the semicolon, will be an
allowed statement even before local variable declarations?
Why do you want to put a macro that expands to a function call prior
to declarations in a block? Both the function call expression and the
empty statement are executable statements. If your implementation
objects to one, it will object to the other. Both are "allowed" under
all versions of the C standard, the issue is whether declarations, of
any sort, are allowed after any executable statements in a block.
For versions of the C standard prior to 1999, they are not. For
versions of the C standard as of 1999 and later, they are.
Thanks!
Please set your newsreader to post a proper signature delimiter, which
consists of "-- " followed by a newline.
I'm not sure I understand what you are trying to do, and why. One
thing you could do is define your macro expansion to include the
terminating semicolon. If you accidentally forget and type a
semicolon after the macro when you use it, it will merely be an empty
statement in the body of the function.
That will cause no harm at all, unless you write code like:
if (some_condition )
FOO(x);
else
/* something */
In that case, the expansion of FOO(x) results in:
if (some_condition )
foo(x);;
else
....but I would not expect a professional engineer with an ieee.org
email address to write such unprofessional code that he would not
fully brace enclose all code controlled by conditional statements.
But again, if the conditional macro is not defined and your compiler
objects to the empty statement, it would object to the function call
expansion of the macro as well, when the conditional is defined, so
what are you hoping to gain?
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html