LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-11-2003, 12:24 PM   #1
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
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?
 
Old 02-11-2003, 02:34 PM   #2
Alberto Pose
LQ Newbie
 
Registered: Feb 2003
Location: Buenos Aires, Argentina
Distribution: Red Hat 7.3
Posts: 1

Rep: Reputation: 0
It's the same...

Maybe asking to Dennis Ritchie....
 
Old 02-11-2003, 02:41 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 ;)
 
Old 02-13-2003, 06:08 AM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 02-13-2003, 11:42 AM   #5
Stuka
LQ Newbie
 
Registered: Feb 2003
Location: Houston, TX
Distribution: Debian/RedHat/Slack
Posts: 12

Rep: Reputation: 0
Actually, there is a HUGE difference between
Code:
void foo()
and
Code:
void foo(void)
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.
 
Old 02-13-2003, 01:41 PM   #6
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Original Poster
Rep: Reputation: 30
"... any number of arbitrarily typed arguments ..."

So how would one access those parameters? Manually pop them backwards off the stack?
 
Old 02-13-2003, 02:14 PM   #7
Stuka
LQ Newbie
 
Registered: Feb 2003
Location: Houston, TX
Distribution: Debian/RedHat/Slack
Posts: 12

Rep: Reputation: 0
lackluster: I wouldn't really know...and I'd shoot anybody I worked with who wrote code like that!
 
Old 02-13-2003, 11:52 PM   #8
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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.
 
Old 02-15-2003, 08:52 AM   #9
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Original Poster
Rep: Reputation: 30
moeminhtun : that is very clear, thank you
 
Old 02-15-2003, 10:57 AM   #10
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
You're welcome.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what (void) and \ and (( means ? cigarstub Programming 2 10-28-2005 04:38 PM
void pointers suchi_s Programming 9 11-08-2004 03:05 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void? Patchorus Programming 9 10-25-2003 07:24 PM
Virtual void Cyth Linux - General 7 01-02-2002 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration