LinuxQuestions.org
Review your favorite Linux distribution.
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 11-18-2005, 08:31 PM   #1
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Rep: Reputation: 31
difference between function declaration: func() and func(void)


What is the difference between these two function declarations in C?

1) void func1();
2) void func2(void);

In a book I am reading, it says that in the first case, the compiler doesn't check the argument list passed to func1(); whereas in the second case func2() is explicitly declared to take no arguments.

I understand what that means but in real life how can the first case be useful?

Thanks.
 
Old 11-18-2005, 10:04 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
I don't think there is a difference.
 
Old 11-19-2005, 01:15 AM   #3
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Re: difference between function declaration: func() and func(void)

Quote:
Originally posted by koyi
the compiler doesn't check the argument list passed to func1()
Thanks.
Do u mean that call such as this wouldn't raise errors-
Code:
func1(x,y);
 
Old 11-19-2005, 09:54 AM   #4
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Original Poster
Rep: Reputation: 31
Alright I have tested it with this piece of simple source code.

Code:
#include <stdio.h>

int func1();
int func2(void);

int main(int argc, char **argv) {

        int a, b;

        a = func1(1, 1, 2);
        b = func2();

        printf("a = %d\tb = %d\n", a, b);

        return 0;

}

int func1(int a, int b) {
        return a + b;
}

int func2(void) {
        return 3;
}
And this compiles cleanly and ran with the output:

Code:
a = 2   b = 3
As you can see, in the declaration func1() is declared as just func1().
In main(), it is called with 3 arguments but in the definition of func1() it only needs two of them.
But no errors occur in both the compilation and execution.
Well, I don't think this is a bug in the compiler or what but I am just wondering how can this be useful in real life.

Btw, if you pass any arguments to func2() the an error will occur when you try to compile it.
 
Old 11-19-2005, 10:19 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
The declaration funct1() is actually funct1(...). main and printf are also declared that way as well. Specificly, the declarations are main(...) and printf(char *format,...). funct1(void) forces no arguments, as your book says.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to find datatype of i in func(void *i,...) kapsikum Programming 2 03-09-2005 09:07 AM
C++ prototyped func with default values ? Bluesuperman Programming 3 02-03-2005 09:20 AM
func to store strs's jnusa Programming 2 11-09-2004 04:29 AM
func. system() ---> perl vs. c rocknix Programming 1 10-17-2003 06:27 AM
ide_dma_timeout func only: 14 MesS Linux - Software 5 10-09-2001 05:37 PM


All times are GMT -5. The time now is 12:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration