4

Since C does not support method overloading, how is it possible to have methods like open, that explicitly offers two different signatures:

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

I mean, printf supports a variate number of arguments using vargs, but have no definite signature other than the one using vargs itself -- otherwise there should be one signature for each possible printf call. Yet, open() -- as I presume -- is written in C, and offers two explicit signatures.

I didn't actually get the way these functions are implemented. Can someone show a small example of how a function like:

void foo() { printf("bar\n"); }
void foo(int x) { printf("bar %d\n", x); }

would be implemented in C?

Community
  • 1
  • 1
Rubens
  • 14,478
  • 11
  • 63
  • 92
  • 1
    I have asked a similar question before, please refer to: [here][1] [1]: http://stackoverflow.com/questions/3953895/why-does-man-2-open-say-this – Amir Zadeh Apr 13 '13 at 20:07
  • @GreenCode I searched for method overloading, but your question did not show up; that's why I've asked. +1 Thanks for the reference. – Rubens Apr 13 '13 at 21:01

5 Answers5

4

The function open is defined with a variable number of arguments:

int open(const char *_path, int _oflag, ...)

Here is a source of <fcntl.h>; the function open is declared at the bottom using the _PROTOTYPE macro.

Note that part of the reason it works with open is that the function takes other parameters already. In order for the function to take variable number of arguments, it must take at least one "fixed" argument. That is why it is not possible to do this trick with your foo() function.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1 Thanks for the explanation. Now I suppose the function `open()`, apart from taking at least one argument, uses some "`assert`ion" for compile-time checking of wrong number of parameters, right? – Rubens Apr 13 '13 at 17:57
  • 1
    @Rubens There is no other compile-time checking the compiler can do: it applies the standard type promotions to the third arguments (and any other args that you may choose to pass, because calling `open` with four, or even forty, arguments, is possible). I suppose there's some run-time checking going on, but the compiler is very limited at what it can check for args beyond `_oflag`. – Sergey Kalinichenko Apr 13 '13 at 18:02
1

You've picked a poor reference (http://linux.die.net) to learn about this function. A better one is the Open Group Base Specifications. And it shows this declaration for open():

int open(const char *path, int oflag, ... );

So in other words, this is just varargs.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

how is it possible to have methods like open, that explicitly offers two different signatures:

Wait, wait, wait... nah. Not even close. How'bout reading the documentation?

open() is a variadic function, its signature is

int open(const char *path, int oflag, ... );

No magic here.

  • on some distros `man 2 open` yields to documentation with these signatures: int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); , hence the confusion. – santileortiz Jan 07 '16 at 07:16
0

You can not define function with the same name more than 1 time in the program as you mentioned in your question

Using macro will allow you to define function more than 1 time in the C code but only 1 function will compiled

#ifdef MACRO1
void foo() { printf("bar\n"); }
#else
void foo(int x) { printf("bar %d\n", x); }
#endif

and in the main

#ifdef MACRO1
    foo();
#else
    foo(5);
#endif
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 1
    But would this allow both the first and the second method calls, as the `open()` function does? I'm not sure, but I guess I can use both calls of `open()` in a main file, can't I? – Rubens Apr 13 '13 at 17:43
0

It is all varargs based. In the case of open, it only reads the third argument for certain inputs and states. In particular, when it has to create the file. Similarly, printf only reads the arguments when it scans the % format markers. Your case doesn't really work that way as there is no early argument to indicate how many arguments there are.

DrC
  • 7,528
  • 1
  • 22
  • 37