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

Variadic macros

P: n/a
I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as

#define SAMPLE_MACRO(...) Bloody-blah

must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?

Feb 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Thomas Carter wrote:
I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as

#define SAMPLE_MACRO(...) Bloody-blah

must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?


From Steele/Harbison

'When such a macro is invoked, there must be as many actual arguments as
there are identifiers in /indentier_list/'

#define name(/indentier_list/, ...) /sequence-of-tokens/

From that I would infer from that, there may indeed be zero actual
parameters passed to 'name'.

This is also how it works in gcc - which may or may not have this right
currently.

However, why would you want this? Unless you wanted to do something like?

#define mkstr(a) a "\0"

#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS__) == '\0' ? "" :
mkstr(__VA_ARGS__))

e.g.,

PRINT(); // does a \n

Expansion:

printf("%s\n", * "\0" == '\0' ? "" : "\0");

PRINT("Boo " "Hoo") // outputs Boo Hoo.

Expansion:

printf("%s\n", * "Boo " "hoo" "\0" == '\0' ? "" : "Boo " "hoo" "\0");

--
==============
*Not a pedant*
==============
Feb 23 '06 #2

P: n/a
On Thu, 23 Feb 2006 14:06:17 +0000, pemo wrote:
Thomas Carter wrote:
I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as

#define SAMPLE_MACRO(...) Bloody-blah

must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?
From Steele/Harbison

'When such a macro is invoked, there must be as many actual arguments as
there are identifiers in /indentier_list/'

#define name(/indentier_list/, ...) /sequence-of-tokens/

From that I would infer from that, there may indeed be zero actual
parameters passed to 'name'.

This is also how it works in gcc - which may or may not have this right
currently.


You are right. When I tested with my original code in gcc I got an error,
because when I invoked my macro without arguments it got expanded into a
syntactically illegal C statement.
However, why would you want this? Unless you wanted to do something
like?

#define mkstr(a) a "\0"

#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS__) == '\0' ? "" :
mkstr(__VA_ARGS__))

e.g.,

PRINT(); // does a \n

Expansion:

printf("%s\n", * "\0" == '\0' ? "" : "\0");

PRINT("Boo " "Hoo") // outputs Boo Hoo.

Expansion:

printf("%s\n", * "Boo " "hoo" "\0" == '\0' ? "" : "Boo " "hoo" "\0");


Feb 23 '06 #3

P: n/a
pemo <us***********@gmail.com> wrote:
Thomas Carter wrote:
I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as

#define SAMPLE_MACRO(...) Bloody-blah

must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?
From Steele/Harbison

'When such a macro is invoked, there must be as many actual arguments as
there are identifiers in /indentier_list/'

#define name(/indentier_list/, ...) /sequence-of-tokens/

From that I would infer from that, there may indeed be zero actual
parameters passed to 'name'.

All true.
This is also how it works in gcc - which may or may not have this right
currently.
IIRC, gcc is different - it removes comma before __VA_ARGS__ if it is
empty, which the Standard specification doesn't. For this reason
it is advised to skip the last mandatory parameter:

int fprintf(FILE *stream, const char *format, ...);

#define FPRINTF(stream, ...) fprintf(stream, __VA_ARGS__) //dropped `format'

(I'm writing from memory, if I'm wrong I hope someone will correct me.)

However, why would you want this? Unless you wanted to do something like?

#define mkstr(a) a "\0"

#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS__) == '\0' ? "" :
mkstr(__VA_ARGS__))
Why not simply:
#define PRINT(...) printf("%s\n", __VA_ARGS__ "")
or even:
#define PRINT(...) puts(__VA_ARGS__ "")
?
PRINT(); // does a \n .... PRINT("Boo " "Hoo") // outputs Boo Hoo.


and
PRINT("Boo ", "Hoo") // outputs diagnostic (by your definition)

I'm not quite sure (no time to check, I once knew but forgot; again
someone please check this), I believe in C99 (in C90 it is UB) macro
arguments may be empty, so perhaps this version might be better:

#define PRINT(slit_opt) puts(slit_opt "")

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Feb 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.