empty macro that won't give compiler errors?

  • Thread starter talkaboutquality
  • Start date
T

talkaboutquality

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
(e-mail address removed)
http://talkaboutquality.wordpress.com
 
J

Jack Klein

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)

....???

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.

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
 
R

Richard Heathfield

talkaboutquality said:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top