macros

J

j0mbolar

given:

#define FOO(x) ((x) + (x))

I recall a thread awhile back saying that
the standard wasn't clear as to what happens if FOO()
were invoked with no arguments. But others were saying
it was clear. Does it lead to undefined behavior or not?
 
J

Jens.Toerring

In comp.lang.c j0mbolar said:
#define FOO(x) ((x) + (x))
I recall a thread awhile back saying that
the standard wasn't clear as to what happens if FOO()
were invoked with no arguments. But others were saying
it was clear. Does it lead to undefined behavior or not?

Since a macro is just doing a text replacement I would expect
the compiler to complain when you write

printf("%d\n", FOO());

and it thus gets, after the preprocessor did its job,

printf("%d\n", (() + ()));

You won't get that far that this could lead to undefined
behavior.
Regards, Jens
 
K

Keith Thompson

given:

#define FOO(x) ((x) + (x))

I recall a thread awhile back saying that
the standard wasn't clear as to what happens if FOO()
were invoked with no arguments. But others were saying
it was clear. Does it lead to undefined behavior or not?

A very quick summary: C99 allows empty macro arguments. The intent of
the standard, stated in a footnote and in the rationale, is that if
FOO is defined to expect one argument, FOO() is actually an invocation
with a single empty argument, not an invocation with no arguments.

We had a very long discussion about whether that intent is adequately
expressed in the normative wording of the standard.

This was discussed at great length. Do a groups.google.com search for
subject "Empty macro arguments" in comp.std.c. Please don't try to
re-kindle the discussion unless you have something new to add.
 
D

Douglas A. Gwyn

Since a macro is just doing a text replacement I would expect
...

Don't try to intuit the requirements; read them.

For C89 the usage would violate a constraint;
for C99 the usage would be taken as providing
an empty argument. The macro expansion then
produces a syntax error.

The behavior is not just undefined, it requires
a diagnostic.
 

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
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top