#ifndef WHATEVER_H
#define WHATEVER_H
void test(const char *format, ...); // would you have to #include <stdarg.h> for ... on argument, or is it ok if you don't use it
#endif // WHATEVER_H
So if I were to have a header file like this, where I needed to put ... as my argument for my void test function, would I have to include stdarg.h for the ... argument, or is it not mandatory?
The header does not need to include <stdarg.h> if the prototypes only include the ellipsis (, ...) notation. The code implementing the test() function will need to include <stdarg.h>, but the header declaring it need not.
However, you should often consider creating a second function, void vtest(const char *format, va_list args); to match the test() function in the header, and then you do need <stdarg.h> to define the va_list type (and the implementation code no longer needs a separate #include <stdarg.h>). With the vtest() declaration in the header, the implementation of the test() function becomes trivial:
void test(const char *format, ...)
{
va_list args;
va_start(args, format);
vtest(format, args);
va_end(args);
}
This is particularly simple since there is no return value to relay, but returning a value is not very much harder. It's often a good idea to implement a variadic function like test() using this scheme, even if you don't expose the vtest() function — it is rather likely that you'll want the extra flexibility it provides eventually.
No you do not need to, but it will rather hard to access parameters hidden behind the ... without macros from this header.
you will need to know your implementation for example for gcc you can use:
__builtin_va_start(v,l)
__builtin_va_end(v)
__builtin_va_arg(v,l)
__builtin_va_copy(d,s)
Related
I'm working on a custom unit-testing framework that doesn't display the filename/line numbers of failed tests. I know I could require them as arguments for the report_results function but that seems like a lot of text.
Is there a way to make it so that __FILE__ and __LINE__ are always passed with the function, even if I don't pass them when I call it?
You can do something like this in C++:
void func(char *file=__FILE__, int line=__LINE__)(int more_args) {}
use a macro in your code, to call an underlying function
#define func(arg) real_func(__FILE__,__LINE__,arg)
where real_func is
void real_func(const char *file, int line, int arg);
In your code, when you call func(12), it expands to a call to real_func, with actual file path and line of the call. You can even support variable argument functions in C99 with variadic macros as a frontend.
As we know printf() function, it can hold characters and if needed could have additional arguments. Ex. printf("Programmer"); printf("Programmer %d",14);
How could I add additional arguments in a prototype function?
Is it something like some_func(char *str, ...) ?
In order to use variable number of arguments in C you need to include the following library
#include <stdarg.h>
printf() in C is an example of function that takes variable number of arguments.
int printf(const char *fmt, ...)
More info here
You have to use the variable argument list provided in C.
Here is a short tutorial on this functionality:
http://www.cprogramming.com/tutorial/lesson17.html
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C/C++: Passing variable number of arguments around
I'm currently using the following macro declared on my C file.
#define COMMON_Print(...) printf (__VA_ARGS__)
Now that call works just fine, but turns out that I need to be able to create a C function that looks something like this:
void COMMON_Print(...)
{
printf (__VA_ARGS__);
}
So that function doesn't work, I get an error
"Error : undefined identifier __VA_ARGS__"
The complexity of my project requires to have a function since it's an interface... So how can I get the parameters ... and pass them to the printf function? Or better what am I doing wrong?
Thanks!
Each of the ?printf functions has a corresponding v?printf function which does the same thing but takes a va_list, a variable argument list.
#include <stdio.h>
#include <stdarg.h>
void COMMON_Print(char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
Side note: Notice that va_start takes the name of the last argument before the ... as a parameter. va_start is a macro which does some stack-based magic to retrieve the variable arguments and it needs the address of the last fixed argument to do this.
As a consequence of this, there has to be at least one fixed argument so you can pass it to va_start. This is why I added the format argument instead of sticking with the simpler COMMON_Print(...) declaration.
See: http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/
__VA_ARGS__ is only for macros; variadic functions are rather different. You need to use va_start, va_arg and va_end from stdarg.h to handle them.
First, your function needs at least one named parameter, e.g.:
void COMMON_Print(const char* fmt, ...)
Then you can define a va_list and use va_start to set it:
va_list args;
va_start(args, fmt);
// your code here
va_end(args);
Now you can use args to access the arguments; calling va_arg(args, TYPE) will return the next variadic argument, so you can just keep calling that until you've read all the arguments.
If you're just trying to call printf, there's a printf variant called vprintf that takes the va_list directly:
vprintf(fmt, args);
There is no easy way to call one variadic function from another; you need something like vprintf that takes the whole va_list
__VA_ARGS__ is for macros only.
Chaining the variable number of argument to another function can't be done directly. Instead you have to
pass a va_list , and the function you're calling have to take a va_list. Luckily there's a variation of printf that does this, your function have to be written this way:
void COMMON_Print(char *format,...)
{
va_list args;
va_start(args,format);
vprintf(format,args);
va_end(args);
}
What you are looking for is the Ellipsis operator.
Suppose I have a function which takes variadic arguments (...) or a va_list passed from another such function. The main logic is in this function itself (let's call it f1), but I want to have it pass the va_list to another function (let's call it f2) which will determine the next argument type, obtain it using va_arg, and properly convert and store it for the caller to use.
Is it sufficient to pass a va_list to f2, or is it necessary to pass a pointer to va_list. Unless va_list is required to be an array type or else store its position data at the location the va_list object points to (rather than in the object itself), I can't see how passing it by value could allow the calling function (f1) to 'see' the changes the called function made by va_arg.
Can anyone shed light on this? I'm interested in what the standard requires, not what some particular implementation allows.
It looks like you'll need to pass a pointer to the va_list. For more info, see the C99 standard document section 7.15.In particular, bullet point 3 states:
The object ap may be passed as an argument to
another function; if that function invokes the va_arg macro with parameter ap, the
value of ap in the calling function is indeterminate and shall be passed to the va_end
macro prior to any further reference to ap
[my italics]
Edit: Just noticed a footnote in the standard:
215) It is permitted to create a pointer to a va_list and pass that pointer to another function, in which
case the original function may make further use of the original list after the other function returns
So you can pass a pointer to the va_list and do va_arg(*va_list_pointer) in the called function.
In my understanding, you're supposed to pass the va_list directly (not a pointer to it). This seems to be supported by comp.lang.c:
"A va_list is not itself a variable argument list; it's really sort of a pointer to one. That is, a function which accepts a va_list is not itself varargs, nor vice versa. "
I find the texts quite ambiguous on this question. The simplest is perhaps to look in the standard how predefined functions with va_list are supposed to receive it, e.g vsnprintf. And this is clearly by value and not by reference.
You should pass a pointer to a va_list if you want to use it in a subfunction and then not have to immediately pass it to va_end afterwards. From C99:
It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns.
The standard allows this, however, on some 64-bit platforms where va_list is an array type, this does not work. As the address of an array is the same as the address of the first element of the array, passing a pointer to the va_list will segfault upon calling va_arg with that pointer to va_list as an argument.
A way to get around this is by receiving the va_list as an unconventional argument name (usually suffixed with one or more underscores) and then creating a new local va_list, like so:
#include <stdarg.h>
int vfoo(va_list ap_)
{
int ret;
va_list ap;
va_copy(ap, ap_);
ret = vbar(&ap);
/* do other stuff with ap */
va_end(ap);
return ret;
}
This is the approach I use in my vsnprintf implementation to call other functions from it for formatting.
Functions in standard C library pass va_list element itself (man 3 vprintf):
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
Passing a pointer to va_list works fine in 32 bit system. You can even fetch one parameter a time in the sub-routine.
But it doesn't seem to work in 64 bit system, will produce a segment fault at va_arg().
I understand that the difference between the printf, fprintf, sprintf etc functions and the vprintf, vfprintf, vsprintf etc functions has to do with how they deal with the function arguments. But how specifically? Is there really any reason to use one over the other? Should I just always use printf as that is a more common thing to see in C, or is there a legitimate reason to pick vprintf instead?
printf() and friends are for normal use. vprintf() and friends are for when you want to write your own printf()-like function. Say you want to write a function to print errors:
int error(char *fmt, ...)
{
int result;
va_list args;
va_start(args, fmt);
// what here?
va_end(args);
return result;
}
You'll notice that you can't pass args to printf(), since printf() takes many arguments, rather than one va_list argument. The vprintf() functions, however, do take a va_list argument instead of a variable number of arguments, so here is the completed version:
int error(char *fmt, ...)
{
int result;
va_list args;
va_start(args, fmt);
fputs("Error: ", stderr);
result = vfprintf(stderr, fmt, args);
va_end(args);
return result;
}
You never want to use vprintf() directly, but it's incredibly handy when you need to e.g. wrap printf(). For these cases, you will define the top-level function with variable arguments (...). Then you'll collect those into a va_list, do your processing, and finally call vprintf() on the va_list to get the printout happening.
The main difficulty with variadic arguments is not that there is a variable number of arguments but that there is no name associated with each argument. The va_start, va_arg macros parse the arguments in memory (in most C compilers they are on the stack) using the type information contained in the format string cf. Kernighan and Ritchie, second edition, section 7.3.
This example shows the elegance of Python. Since C/C++ cannot reconcile the difference between int error(char *fmt, ...) and int error(char *fmt, va_list ap), thus, for every function *printf, it has to create two versions, i.e., one taking in ..., the other taking in va_list, this essentially doubles the total number of functions. In Python, you can use *list() or **dict() to pass in a va_list as ....
Hopefully, future C/C++ can support this kind of argument processing scheme.