Today, looking at the man page for open(), I’ve noticed this function is ‘overloaded’:
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
I didn’t thought it’s possible on C. What’s the ‘trick’ for achieving this ?
LATER EDIT:
So it’s not really overloading, because when using varargs – you can only supply multiple arguments of the same type. So, is mode_t behind the scenes an int ?
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.
With variable arguments, the arguments don’t need to be the same type.
printfis the obvious example of this.In the case of open(), the first variable argument have to be mode_t if ‘
flagscontain 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)