473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

empty macro arguments

The following code compiles and works as expected when
compiled with gcc.

/* empty-args.c */

#include <stdio.h>

#define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"")
#define bar(x) puts("foo: x=\"" #x "\"")

int main(void)
{
foo(,);
foo(FOO,);
foo(,BAR);
foo(FOO,BAR);
bar();
bar(FOO);
return 0;
}

However, when I compiled it with a different compiler, I received
the following compiler messages:

| cpp: empty-args.c:14 Disagreement in number of macro arguments
| Warning empty-args.c: 14 missing prototype for bar
| Warning empty-args.c: 14 Missing prototype for 'bar'
| 0 errors, 3 warnings
| empty-args.obj .text: undefined reference to '_bar'
| linker returned 1

It seems there are no problems with 'foo' if one or both arguments
are empty, but the same is not true for 'bar'. Is this an extension
in gcc? A new thing in C99?

What does the standard say? (I'm not that good at standardese)

Jun 27 '08 #1
9 7607
Two-Horned Unicorn <two_horned.unicorn@yahoo_NOSPAM_.comwrites:
#define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"")
#define bar(x) puts("foo: x=\"" #x "\"")
(...)
foo(,);
foo(FOO,);
foo(,BAR);
ANSI C section 3.8.3: "If (before argument substitution) any argument
consists of no preprocessing tokens, the behavior is undefined."
So gcc is free to accept it.
bar();
The gcc folks say this is a macro invocation with an empty argument:
<http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11233>. Maybe your other
compiler vendor disagrees. In any case, as above it is undefined
behavior.

--
Hallvard
Jun 27 '08 #2
Hallvard B Furuseth wrote:
Two-Horned Unicorn <two_horned.unicorn@yahoo_NOSPAM_.comwrites:
>#define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"")
#define bar(x) puts("foo: x=\"" #x "\"")
(...)
foo(,);
foo(FOO,);
foo(,BAR);

ANSI C section 3.8.3: "If (before argument substitution) any argument
consists of no preprocessing tokens, the behavior is undefined."
So gcc is free to accept it.
That was true of C89, but is not true of C99:
6.10.3 Macro Replacement, p4:

"If the identifier-list in the macro definition does not end with an
ellipsis, the number of arguments (including those arguments consisting of
no preprocessing tokens) in an invocation of a function-like macro shall
equal the number of parameters in the macro definition."
>
> bar();

The gcc folks say this is a macro invocation with an empty argument:
<http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11233>. Maybe your other
compiler vendor disagrees.
This is a rather subtle change in C that was discussed a while ago in
comp.std.c. A macro invocation with no arguments matches a function-like
macro with either 0 or 1 parameters in C99.

--
Thad
Jun 27 '08 #3
Hallvard B Furuseth <h.**********@usit.uio.nowrites:
Two-Horned Unicorn <two_horned.unicorn@yahoo_NOSPAM_.comwrites:
>#define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"")
#define bar(x) puts("foo: x=\"" #x "\"")
(...)
foo(,);
foo(FOO,);
foo(,BAR);

ANSI C section 3.8.3: "If (before argument substitution) any argument
consists of no preprocessing tokens, the behavior is undefined."
That text has gone from C99 and there are several examples of empty
arguments later on to drive home the point.

lcc-win32 (the original compiler whose messages started the thread)
makes no claim to support C99 so it is free to take the older point of
view, but oddly the pre-processors tries to implement __VA_ARGS__
suggesting an intent to be C99 conforming.

--
Ben.
Jun 27 '08 #4
Ben Bacarisse <be********@bsb.me.ukwrites:
[...]
lcc-win32 (the original compiler whose messages started the thread)
makes no claim to support C99 so it is free to take the older point of
view, but oddly the pre-processors tries to implement __VA_ARGS__
suggesting an intent to be C99 conforming.
I thought lcc-win32's author *did* claim that it supports C99, or at
least most of it with a couple of exceptions. But I think variadic
macros were one of the exceptions. It's probably a work in progress.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #5
Keith Thompson <ks***@mib.orgwrites:
Ben Bacarisse <be********@bsb.me.ukwrites:
[...]
>lcc-win32 (the original compiler whose messages started the thread)
makes no claim to support C99 so it is free to take the older point of
view, but oddly the pre-processors tries to implement __VA_ARGS__
suggesting an intent to be C99 conforming.

I thought lcc-win32's author *did* claim that it supports C99, or at
least most of it with a couple of exceptions. But I think variadic
macros were one of the exceptions. It's probably a work in
progress.
I don't recall any clear statement on current or planned conformance
and I concluded that that was the way Jacob wanted it. Having C99
conformance as an objective for some unspecified time in the future
means there are fewer bugs now. If I have that wrong I hope he will
clear the matter up.

--
Ben.
Jun 27 '08 #6
Ben Bacarisse wrote, On 09/06/08 12:40:
Keith Thompson <ks***@mib.orgwrites:
>Ben Bacarisse <be********@bsb.me.ukwrites:
[...]
>>lcc-win32 (the original compiler whose messages started the thread)
makes no claim to support C99 so it is free to take the older point of
view, but oddly the pre-processors tries to implement __VA_ARGS__
suggesting an intent to be C99 conforming.
I thought lcc-win32's author *did* claim that it supports C99, or at
least most of it with a couple of exceptions. But I think variadic
macros were one of the exceptions. It's probably a work in
progress.

I don't recall any clear statement on current or planned conformance
and I concluded that that was the way Jacob wanted it. Having C99
conformance as an objective for some unspecified time in the future
means there are fewer bugs now. If I have that wrong I hope he will
clear the matter up.
He has made it pretty clear that it is not intended to conform to
C90/C95 (this is *not* a complaint, merely a comment) and that he
personally wants everyone to go C99 so I think the only standard we can
in fairness check it against is C99. This means that it is correct for
it to behave with the C99 rules for variadic macros.
--
Flash Gordon
Jun 27 '08 #7
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Ben Bacarisse wrote, On 09/06/08 12:40:
>Keith Thompson <ks***@mib.orgwrites:
>>Ben Bacarisse <be********@bsb.me.ukwrites:
[...]
lcc-win32 (the original compiler whose messages started the thread)
makes no claim to support C99 so it is free to take the older point of
view, but oddly the pre-processors tries to implement __VA_ARGS__
suggesting an intent to be C99 conforming.
I thought lcc-win32's author *did* claim that it supports C99, or at
least most of it with a couple of exceptions. But I think variadic
macros were one of the exceptions. It's probably a work in
progress.

I don't recall any clear statement on current or planned conformance
and I concluded that that was the way Jacob wanted it. Having C99
conformance as an objective for some unspecified time in the future
means there are fewer bugs now. If I have that wrong I hope he will
clear the matter up.

He has made it pretty clear that it is not intended to conform to
C90/C95 (this is *not* a complaint, merely a comment) and that he
personally wants everyone to go C99 so I think the only standard we
can in fairness check it against is C99. This means that it is correct
for it to behave with the C99 rules for variadic macros.
You mean, presumably, that it *would* be correct for it to behave with
the C99 rules for variadic macros. The forward in the standard lists
only two changes that relate to macros: variable number of arguments
and empty arguments. The lcc-win32 I have gets both of these wrong:

#define ONE(x) #x
#define MANY(...) [__VA_ARGS__]
ONE()
MANY(abc, def, ghi)

Produces:

ONE()
[abc,def,ghi)

along with a warning about the empty argument. The C99 rules may be
the ones aspired to but there is a way to go yet.

--
Ben.
Jun 27 '08 #8
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
>The lcc-win32 I have gets both of these wrong:
>#define ONE(x) #x
#define MANY(...) [__VA_ARGS__]
ONE()
MANY(abc, def, ghi)
>Produces:
>ONE()
[abc,def,ghi)
>along with a warning about the empty argument.
Was that a typo, or does it truly end the list with ) instead of ] ??
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Jun 27 '08 #9
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
>>The lcc-win32 I have gets both of these wrong:
>>#define ONE(x) #x
#define MANY(...) [__VA_ARGS__]
ONE()
MANY(abc, def, ghi)
>>Produces:
>>ONE()
[abc,def,ghi)
>>along with a warning about the empty argument.

Was that a typo, or does it truly end the list with ) instead of ]
??
No typo. It was a file include (and I then cut the file name that
appears in a #line directive).

It is different with stringise. I've removed the []s to make it
clearer. This input:

--- macros.c ---
#define MANY(...) __VA_ARGS__
#define STRING(...) #__VA_ARGS__
MANY(abc, def, ghi)
STRING(abc, def, ghi)
----------------

Produces:
--- macros.i ---
#line 1 "Z:\home\ben\play\lcc\macros.c"
abc,def,ghi)
"abc"
----------------

and this:

--- at-end.c ---
#define MANY_1(x, ...) __VA_ARGS__, x
MANY_1(abc, def, ghi)
----------------

crashes the compiler:

Error (null) 0 Compiler error (trap). Stopping compilation
1 error

--
Ben.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include...
10
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
14
by: Michael B Allen | last post by:
I just noticed that doing something like the following may fail because it can overwrite u->size before it's evaluated. memcpy(u, buf, u->size); Is it legit that this is a macro as opposed to...
15
by: Urs Thuermann | last post by:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this: int i; char *s; DBG("simple message\n"); DBG("message with an int...
4
by: Max TenEyck Woodbury | last post by:
I need a macro that will take an arbitrarily long list of arguments where each argument needs to be passed to another macro. Is it possible to write such a macro? If so, could you please...
9
by: Francois Grieu | last post by:
Consider this macro // check if x, assumed of type unsigned char, is in range #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can't be safely used as in if (ISVALID(*p++)) foo();...
2
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.