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.
I've never had a compiler balk at an empty set of parentheses. It's widely assumed that:
void foo()
takes no arguments. Using
void foo(void)
just makes it explicit that the function takes no arguments. But it's so common for a function to have no arguments that the first syntax is usually okay too. (Are there compilers that complain about this? g++ has never said anything to me about it )
Anyway, the result is the same no matter which you use, unless you have a compiler that for some reason assumes that foo() means a function that takes an int argument, or something.
But yes, Tinkster is right - it is best to be absolutely explicit! Don't assume anything in programming.
in C (or at least the older standards...don't know about C99). The former indicates a function which can take any number of arbitrarily typed arguments, while the latter is a function that takes no arguments. In C++, the two mean the same thing - no arguments at all.
They are different if they are function prototype. If they are function definitions, there is no difference.
If you declare a function prototype with empty argument (without void), you can have one or more integer or any type of pointer arguments in your function definitions.
For example,
If this is your function prototype,
void foo();
Then these are the valid function definitions.
void foo(int i)
void foo(int a, int b, int c)
void foo(char* c, int b)
etc..
But If you declare the function prototype "with void", your function definition must not have any arguments.
Here is an example.
<code>
#include <stdio.h>
void myFunction();
main() {
int i = 3;
char a = 'A';
printf("Address of i is ");
myFunction(&i);
printf("Value of i is ");
myFunction(i);
printf("Address of a is ");
myFunction(&a);
printf("Value of a is ");
myFunction(a);
return 0;
}
void myFunction(int a)
{
printf(" %d\n", a);
}
</code>
In the above example, the "myFunction" function can be passed any type of data without casting. But If you change the function prototype with (int), you won't be able to compile unless you cast each different parameter type to integer type explicitly in the function call. The function prototype with (void) will not be compiled too.
Last edited by moeminhtun; 02-14-2003 at 12:04 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.