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? 4 13561
"Augustus S.F.X Van Dusen" <as****@story.n et> 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 ->
--
On Tue, 03 Aug 2004 16:20:23 +0000, Mark A. Odell wrote: "Augustus S.F.X Van Dusen" <as****@story.n et> 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.
In article <pa************ *************** *@story.net>,
Augustus S.F.X Van Dusen <as****@story.n et> 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_fil e, __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_fil e, 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.
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.n et> 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_fil e, __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_fil e, 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. Say, min
function which will take N arguments. Any approaches like generative
programming will help?
-Ganesh
|
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 using the
elipse. For example, a funciton could take in 1 known type, then a
variable list of other parameters:
ReturnValueClass rvc =...
|
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 used like this:
trace( "Err", errtType, lineNum, NULL) /* where errType is char* and
lineNum is
an int */
|
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...
}
|
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 example, if A
is a wrapper function around B).
Thanks,
Nimmi
| |
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
|
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,...)
|
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 MY_MACRO(int mid,int mlevel,...)
my_func(mid,mlevel,format,##args)
void my_func(int mid,int mlevel,char *format,....)
{
va_list ap;
|
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 simple example,
def fun1(**kwargs):
print kwargs.keys()
def fun2(**kwargs):
|
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
numbers (doubles), and the functions only work in one dimension. I
would ideally like to extend them to work in N dimensions, which means
I want...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |