473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what is the difference, void func(void) and void fucn()

Hi all,
What is the difference between following two function definations?
<1>
void func(void)
{
/* some code */
}

<2>
void func()
{
/* some code */
}

Why would somebody use 2nd one?

--santosh.

Nov 15 '05 #1
5 2518
no**********@yahoo.com wrote:
Hi all,
What is the difference between following two function definations?
<1>
void func(void)
{
/* some code */
}

<2>
void func()
{
/* some code */
}
Both define a function returning void, and accepting zero arguments.

They define `func' with different types. The first type is a type
with a prototype, the second type is without a prototype.

If in a function call expression the expression designating the
function has a type without a prototype, and any arguments
are passed, compiler is not required to diagnose an error,
but the behaviour is undefined.

void f();
f(x); //UB, no diagnostic

void f(void);
f(x); //diagnostic

(It's a bit longer story when a function hasn't a prototype, and
has arguments - then the number of arguments and types after
promotion count.)
Why would somebody use 2nd one?


For me it's usually laziness. Maybe there are other reasons,
I don't know.
(Note: there may be important reasons to declare a function
_type_ without a prototype.)

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #2


S.Tobias wrote:
no**********@yahoo.com wrote:
Hi all,
What is the difference between following two function definations?
<1>
void func(void)
{
/* some code */
}

<2>
void func()
{
/* some code */
}


Both define a function returning void, and accepting zero arguments.

They define `func' with different types. The first type is a type
with a prototype, the second type is without a prototype.

If in a function call expression the expression designating the
function has a type without a prototype, and any arguments
are passed, compiler is not required to diagnose an error,
but the behaviour is undefined.

void f();
f(x); //UB, no diagnostic

void f(void);
f(x); //diagnostic

(It's a bit longer story when a function hasn't a prototype, and
has arguments - then the number of arguments and types after
promotion count.)
Why would somebody use 2nd one?


For me it's usually laziness. Maybe there are other reasons,
I don't know.
(Note: there may be important reasons to declare a function
_type_ without a prototype.)

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`


Thanks stan.
and how the following function differs with above two functions

3>
void fucn(int a, ...)
{
/*some code*/
}
Can't we use 2nd as variable number of arguments like 3rd.
Can you or anybody further explore it?
--santosh

Nov 15 '05 #3
In article <3k************@individual.net> "S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:
....
If in a function call expression the expression designating the
function has a type without a prototype, and any arguments
are passed, compiler is not required to diagnose an error,
but the behaviour is undefined.

void f();
f(x); //UB, no diagnostic
This is only undefined behaviour when the actual function f has not a
single parameter. "void f();" declares a function f that returns void
with an unspecified number of parameters of unspecified type. So
f(x) is undefined behaviour if the actual function does *not* have a
single parameter.
Why would somebody use 2nd one?


[ about void f(); ]
For me it's usually laziness. Maybe there are other reasons,


In pre-standard C (i.e. C without prototypes) that was the standard
way to declare functions.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #4
Dik T. Winter <Di********@cwi.nl> wrote:
In article <3k************@individual.net> "S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:
...

void f();
f(x); //UB, no diagnostic


This is only undefined behaviour when the actual function f has not a
single parameter. "void f();" declares a function f that returns void
with an unspecified number of parameters of unspecified type. So
f(x) is undefined behaviour if the actual function does *not* have a
single parameter.


Yes, thanks. I was thinking only about the simple case that
the OP asked about, and should have written: `void f(){}'.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #5
no**********@yahoo.com wrote:
S.Tobias wrote:
no**********@yahoo.com wrote:
> What is the difference between following two function definations?
> <1>
> void func(void)
> {
> /* some code */
> }
>
> <2>
> void func()
> {
> /* some code */
> }
Both define a function returning void, and accepting zero arguments.

They define `func' with different types. The first type is a type
with a prototype, the second type is without a prototype.

[snip] and how the following function differs with above two functions

3>
void fucn(int a, ...)
{
/*some code*/
}
Can't we use 2nd as variable number of arguments like 3rd.


No, the third declares a type wich is not compatible with the second
case.

typedef void f_t();

`f_t' is compatible with:

void f();
void f(void);
void f(int);
void f(int, int);

but is not compatible with:

int f();
void f(int, ...);

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pushkar Pradhan | last post by:
I need to time my blocked matrix multiply code to decide the optimal block size. I know two ways to do it: time() and clock() functions. In order to get time is usec clock() must be divided by...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
7
by: Umesh | last post by:
what this statement &(*IR)->func means. where IR is the pointer to structure & func is pointer to fuction. func is the member of structure.
2
by: Marcus Kwok | last post by:
Is there a difference between using the C++ keyword "void" and using "System::Void" when specifying parameters and return types? Is there a preference when choosing between the two? In the code...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
2
by: Steven T. Hatton | last post by:
In this example: void print(int i) { std::cout<<i<<" "; } for_each(coll.begin(), coll.end(), print); What kind of entity is "print" when it appears as an actual parameter in the for_each...
6
by: Shraddha | last post by:
What is this exactly... int(*(*ptr (int))(void) First I thought that this is the pointer to function...But I recognize that the syntax iss quite different... If we say that the function is...
28
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.