By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

empty macro that won't give compiler errors?

P: n/a
Need to define a macro as, say,

#ifdef NEED_FUNCTION

foo(x)

#else

#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)?

If so, why?

If not, why not?

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?

Thanks!

Tom Harris
t.******@ieee.org
http://talkaboutquality.wordpress.com

Oct 5 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 05 Oct 2007 02:27:41 +0200, talkaboutquality
<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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 5 '07 #2

P: n/a
talkaboutquality said:

<snip>
>
When NEED_FUNCTION is defined, then compiler sees

foo(x);

When NEED_FUNCTION is not defined, compiler sees

;
#ifdef NEED_FUNCTION
#define FOO(x) foo(x)
#else
#define FOO(x)
#endif

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)?
Yes in C90 (which is what you're actually using).
If so, why?
Because an empty statement is still a statement, and you can't mix
statements with declarations.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.