ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In a language that support overriding (which C does not), this usually means that you can redefine an existing function, exactly as it is declared, and define it as you like. For example, let's say that language X defines the print function as:
Code:
# ficticious language X print function
int print(va_arg_list) {
loop_through(va_arg_list) {
stdout.write(va_arg_list.loopitem);
}
}
You could potentially rewrite the function, something like:
About function overriding in C.
I also thought that that's not possible, but then, how come this is possible?
Code:
shell$ man fcntl
[...]
SYNOPSIS
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
E.G. 3 different prototypes for fcntl(), and all of them work.
To me, this looks like a (limited?) form of function overriding (in C).
I've been wondering before how this is achieved. Anyone?
Because C doesn't require that you pass all parameters to the function if you leave the parameter list blank in the prototype. The compiler should only throw up warnings if the prototype has a non-empty parameter list and you don't pass enough enough arguments to the function.
Code:
itsme@dreams:~/C$ cat param.c
#include <stdio.h>
void func();
int main(void)
{
func(1);
func(17, 5);
return 0;
}
void func(int a, int b)
{
if(a == 1)
puts("Only going to use first argument.");
else
printf("Using both arguments: a = %d, b = %d\n", a, b);
}
itsme@dreams:~/C$ gcc -Wall -ansi -pedantic param.c -o param
itsme@dreams:~/C$ ./param
Only going to use first argument.
Using both arguments: a = 17, b = 5
I'm not sure that's how fcntl() does it, but that's one way.
actually fcnt is defined in the header file using
int fcntl (int fd, int cmd, ...);
This means it can take variable number of arguments.
now it looks at the command and then decides if anything follows as the third parameter or not.
see man va_arg etc for more details.
Originally posted by Hko About function overriding in C.
I also thought that that's not possible, but then, how come this is possible?
Code:
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
E.G. 3 different prototypes for fcntl(), and all of them work.
To me, this looks like a (limited?) form of function overriding (in C).
I've been wondering before how this is achieved. Anyone?
Overloading, actually. Overriding is when you modify the behavior of an existing function. Overloading is when you have one function name that is defined with different parameter options. As zeropash mentioned, fcntl is defined with a variable argument list. This is similar to overloading, but not really the same thing. C does not support overriding or overloading, however.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.