I read that C doesn't suppose function overloading. But in this Slide we can see it's not correct and my professor said: "How Is it possible that we have 2 different signatures for same function name in C?"
Can someone explain this?
It isn't possible. Code such as this:
int open(const char* path, int flags);
int open(const char* path, int flags, mode_t mode);
is invalid C and will not compile (but valid C++).
However, C supports variadic functions and the old open
function is implemented using that. It's actually declared as:
int open(const char *path, int oflag, ... );
Where the ...
allows a variable amount of arguments through the features of stdarg.h
.
open
function in this respect is more likeprintf
: a single function that accepts a variable number of arguments. Forprintf
, the number of%
-specifiers in the format string tellsprintf
how many more arguments to expect. Foropen
, the presence of theO_CREAT
bit inflags
tells it whether to expect the thirdmode
argument. – Steve Summit