Today, looking at the man page for open()
, I\'ve noticed this function is \'overloaded\':
int open(const char *pathname, int flags);
int o
"mode must be specified when O_CREAT is in the flags, and is ignored otherwise."
extern int open (__const char *__file, int __oflag, ...)
It uses varargs
and only loads the mode variable argument if __oflag
contains O_CREAT
.
It's using variable arguments. Those declarations appear only in the man page, as those 2 are the only ways you should call open(). The actual C function will be declared as e.g.
int open(const char *pathname,int flags,...);
With variable arguments, the arguments don't need to be the same type. printf
is the obvious example of this.
In the case of open(), the first variable argument have to be mode_t if 'flags
contain the O_CREAT flag because implementation of open() expects it to be mode_t (which behind the scenes is likely an unsigned int or unsigned long - but that has nothing to do with varargs)
very short answer - varargs
C does make it possible to write function with a variable number of argument, such as printf
.
With that being said, there is no reliable, cross-platform way in C to write a function that takes exactly 2 or 3 arguments; in general you must do something like
some_function(5, 6, 7, NULL);
some_function(5, 6, 8, 2, 5, NULL);
In other words, you must have a terminating "sentinal" argument. Alternatively, you could include the number of parameters somehow in an earlier parameter, such as
another_func(2, "hello", "world");
another_func(3, "goodbye", "cruel", "world");
The printf
family of functions take this approach; the first format parameter contains the number of extra parameter needed; e.g. with printf("%f %f", 5.6, 7.11)
you know that there must be 2 float parameters. However, this would be somewhat unsafe in a user-defined library function, since if you said my_printf("%s %f %f %f %s", 5.6)
then you could get segfaults or worse. Fortunately, most C compilers will check your calls to printf
at compile time to avoid this kind of issue.
In the case of open
, the function is declared as having variable arguments, and the third parameter is only checked if O_CREAT
is set. So this is how it "safely" determines whether a third argument is present. I put "safely" in quotes because technically there's no way for open to know at runtime how many parameters were actually passed. For example, the following calls would compile without any errors or warnings:
open("foo.txt", 5, "not an integer", 7); // extra and invalid parameters
open("bar.txt", O_CREAT); // third parameter is missing
you can fake it using variable arguments list with ...
int function(int x, ...);