473,418 Members | 1,980 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,418 software developers and data experts.

Macros with a variable number of arguments

I have recently come across the following construction:

#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)

which can be invoked as follows:

int x = 1, y = 2 ;
char * str = "String" ;

P_VAR(stdout, "x: %d", x) ;
P_VAR(stdout, "y: %d", y) ;
P_VAR(stderr, "str: %s, x: %d, y: %d", str, x, y) ;

printing

This is x: 1
This is y: 2
This is str: String, x: 1, y: 2

Is this ANSI C? Can it be generalized to work with just two arguments?
That is, when there are no optional arguments args?

Nov 14 '05 #1
4 13546
"Augustus S.F.X Van Dusen" <as****@story.net> wrote in
news:pa****************************@story.net:
I have recently come across the following construction:

#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)
Is this ANSI C?


No. I think it is specific to gcc.

--
- Mark ->
--
Nov 14 '05 #2
On Tue, 03 Aug 2004 16:20:23 +0000, Mark A. Odell wrote:
"Augustus S.F.X Van Dusen" <as****@story.net> wrote in
news:pa****************************@story.net:
I have recently come across the following construction:

#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)


Is this ANSI C?


No. I think it is specific to gcc.


And other compilers: I saw it first using the C compiler under HP-UX.

Nov 14 '05 #3
In article <pa****************************@story.net>,
Augustus S.F.X Van Dusen <as****@story.net> wrote:
I have recently come across the following construction:

#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)

which can be invoked as follows:

int x = 1, y = 2 ;
char * str = "String" ;

P_VAR(stdout, "x: %d", x) ;
P_VAR(stdout, "y: %d", y) ;
P_VAR(stderr, "str: %s, x: %d, y: %d", str, x, y) ;

printing

This is x: 1
This is y: 2
This is str: String, x: 1, y: 2

Is this ANSI C? Can it be generalized to work with just two arguments?
That is, when there are no optional arguments args?


Yes and No.

The specific syntax being used is GCC specific. ANSI C (1999 standard)
has variable argument macros with a different syntax (which modern GCC
supports).

Old GCC syntax...
#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)

The "##" in the above macro is GCCs method of dealing with the case where
the variable part of the macro is empty. It has the effect of deleting the
comma just prior to the "##" when args is empty.
ANSI C and new GCC syntax
#define P_VAR(output, ...) \
fprintf (output, "This is "string"\n", __VA_ARGS__)

This definition of P_VAR has the identical expansion as the previous definition
of P_VAR. Both require a minimum of 2 parameters when being expanded.

The Old GCC version has the advantage that it's closer to what you expect
from experience with printf() and company, but suffers from the kludge
needed when the variable part of the macro is empty.

The ANSI C version doesn't have the "empty kludge", but requires one more
parameter than the number of named parameters. For example let's say that
I want to create a "log" macro that is used in the exact same fashion as
printf() except it sends all output to log_file instead of stdout. The
desired macro would look like:

#define log(...) fprintf(log_file, __VA_ARGS__)

And yes, the log macro would require at least 1 parameter in order to be
legal just as printf() requires at least 1 parameter.

Using the old GCC style the macro would be

#define log(format, args...) fprintf(log_file, format, ##args)

I would suggest that you use the new ANSI method of variable argument macros
instead of the old GCC style since it is likely to be supported by more
compilers in the future.
Nov 14 '05 #4
Thanks so much for your answer. The __VAR_ARGS__ approach, that I was not
aware of, is more appropriate to my purposes.
On Tue, 03 Aug 2004 17:00:12+0000, John Cochran wrote:
In article <pa****************************@story.net>,
Augustus S.F.X Van Dusen <as****@story.net> wrote:
I have recently come across the following construction:

#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)

which can be invoked as follows:

int x = 1, y = 2 ;
char * str = "String" ;

P_VAR(stdout, "x: %d", x) ;
P_VAR(stdout, "y: %d", y) ;
P_VAR(stderr, "str: %s, x: %d, y: %d", str, x, y) ;

printing

This is x: 1
This is y: 2
This is str: String, x: 1, y: 2

Is this ANSI C? Can it be generalized to work with just two arguments?
That is, when there are no optional arguments args?


Yes and No.

The specific syntax being used is GCC specific. ANSI C (1999 standard)
has variable argument macros with a different syntax (which modern GCC
supports).

Old GCC syntax...
#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)

The "##" in the above macro is GCCs method of dealing with the case where
the variable part of the macro is empty. It has the effect of deleting the
comma just prior to the "##" when args is empty.
ANSI C and new GCC syntax
#define P_VAR(output, ...) \
fprintf (output, "This is "string"\n", __VA_ARGS__)

This definition of P_VAR has the identical expansion as the previous definition
of P_VAR. Both require a minimum of 2 parameters when being expanded.

The Old GCC version has the advantage that it's closer to what you expect
from experience with printf() and company, but suffers from the kludge
needed when the variable part of the macro is empty.

The ANSI C version doesn't have the "empty kludge", but requires one more
parameter than the number of named parameters. For example let's say that
I want to create a "log" macro that is used in the exact same fashion as
printf() except it sends all output to log_file instead of stdout. The
desired macro would look like:

#define log(...) fprintf(log_file, __VA_ARGS__)

And yes, the log macro would require at least 1 parameter in order to be
legal just as printf() requires at least 1 parameter.

Using the old GCC style the macro would be

#define log(format, args...) fprintf(log_file, format, ##args)

I would suggest that you use the new ANSI method of variable argument macros
instead of the old GCC style since it is likely to be supported by more
compilers in the future.


Nov 14 '05 #5

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

Similar topics

7
by: Ganny | last post by:
Is it possible to write a template function min that takes variable number of arguments? It should be without using ugly C var args, arrays, or statically overloading the method for N arguments....
10
by: Clay_Culver | last post by:
I have heard that it is possible to use classes + template magic to make standalone functions (or maybe just a functor which acts like a function) which can accept variable length arguments without...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
2
by: EricLin | last post by:
Dear all: I am just concerned about how to use a function with variable number of arguments. for example: int TestFunction(arguments) { .... Testing contents... }
3
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For...
7
by: vivekian | last post by:
Trying to write a function which can accept variable number of arguments of the same data type , ranging from 1 ... n . What would be the best way to go about this. Thanks, vivekian
10
by: Praveen.Kumar.SP | last post by:
Hi Could anyone solve the problem for the code below The Code: #include "stdio.h" #include "iostream.h" void Temp( int a, char* str,...)
6
by: rashmi | last post by:
Hello All, Can we map a MACRO with variable number of arguments to a function with variable number of arguments? Please help me in finding out how this could be done ? for eg: #define...
2
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this...
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
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?
0
marktang
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.