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? 3 2563
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*
==============
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");
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` This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jois.de.vivre |
last post by:
Hi,
I'm writing a C++ program that uses C style macros for debugging
purposes. I have code that looks something like this:
#define DEBUG(str, ...) Show(__FILE__, __LINE__, str, ##...
|
by: Trent Buck |
last post by:
(Note: C99 supports variadic macros, but C89 does not.)
I'm pretty sure what I'm trying to do is impossible, but I'll ask here
in case I'm missing something.
I'm trying to define generic,...
|
by: Ross A. Finlayson |
last post by:
Hi,
I hope you can help me understand the varargs facility.
Say I am programming in ISO C including stdarg.h and I declare a
function as so:
void log_printf(const char* logfilename, const...
|
by: Michael B Allen |
last post by:
If I define a variadic macro like say:
#define PRINT(fmt, ...) _myprintf(__FILE__ ": " fmt, __VA_ARGS__)
and I call this like:
PRINT("no args");
the preprocessor generates:
|
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...
|
by: jontwang |
last post by:
How do you get a count of the number of arguments passed into a
variadic macro?
|
by: Laurent Deniau |
last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following
macros expand to:
#define A(...) __VA_ARGS__
#define B(x,...) __VA_ARGS__
A() -nothing, *no warning*
A(x) -x
...
|
by: CryptiqueGuy |
last post by:
Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this...
|
by: Christof Warlich |
last post by:
Hi,
is there any way to access individual elements in the body
of a variadic macro? I only found __VA_ARGS__, which always
expands to the complete list.
Thanks for any help,
Christof
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |