Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-11-2003, 12:24 PM
|
#1
|
Member
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488
Rep:
|
void foo(void) and void foo()
What is the difference between
void foo(void);
and
void foo();
? I used to think they were the same but a long time ago somebody said they're not, and I don't remember why. Anyone know?
|
|
|
02-11-2003, 02:34 PM
|
#2
|
LQ Newbie
Registered: Feb 2003
Location: Buenos Aires, Argentina
Distribution: Red Hat 7.3
Posts: 1
Rep:
|
It's the same...
Maybe asking to Dennis Ritchie.... 
|
|
|
02-11-2003, 02:41 PM
|
#3
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Re: void foo(void) and void foo()
Quote:
Originally posted by lackluster
What is the difference between
void foo(void);
and
void foo();
? I used to think they were the same but a long time ago somebody said they're not, and I don't remember why. Anyone know?
|
Depends on what you compile them with.
C++ should bitch about the missing void :)
A K&R compiler will accept both.
Cheers,
Tink
P.S.: C++ is right ;)
|
|
|
02-13-2003, 06:08 AM
|
#4
|
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018
Rep:
|
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.
|
|
|
02-13-2003, 11:42 AM
|
#5
|
LQ Newbie
Registered: Feb 2003
Location: Houston, TX
Distribution: Debian/RedHat/Slack
Posts: 12
Rep:
|
Actually, there is a HUGE difference between and 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.
|
|
|
02-13-2003, 01:41 PM
|
#6
|
Member
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488
Original Poster
Rep:
|
"... any number of arbitrarily typed arguments ..."
So how would one access those parameters? Manually pop them backwards off the stack?
|
|
|
02-13-2003, 02:14 PM
|
#7
|
LQ Newbie
Registered: Feb 2003
Location: Houston, TX
Distribution: Debian/RedHat/Slack
Posts: 12
Rep:
|
lackluster: I wouldn't really know...and I'd shoot anybody I worked with who wrote code like that!
|
|
|
02-13-2003, 11:52 PM
|
#8
|
Member
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647
Rep:
|
void foo(void);
and
void foo();
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.
|
|
|
02-15-2003, 08:52 AM
|
#9
|
Member
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488
Original Poster
Rep:
|
moeminhtun : that is very clear, thank you 
|
|
|
02-15-2003, 10:57 AM
|
#10
|
Member
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647
Rep:
|
You're welcome. 
|
|
|
All times are GMT -5. The time now is 08:07 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|