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?

2
  • 1
    I have asked a similar question before, please refer to: [here][1] [1]: stackoverflow.com/questions/3953895/… Commented Apr 13, 2013 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. Commented Apr 13, 2013 at 21:01

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Thanks for the explanation. Now I suppose the function open(), apart from taking at least one argument, uses some "assertion" for compile-time checking of wrong number of parameters, right?
@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.
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.

Comments

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.

1 Comment

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.
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.

Comments

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 

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.