473,511 Members | 14,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the usage of void argument

Hi,

I find I have to declare a function like "int f(void);", if the
function should not accept any arguments. If I declare with "int f();"
instead, the compiler won't give any error if I call the function f
with arguments.

I'm wondering when I should use "f()" instead of "f(void)" to declare
the function. Should I use "f(void)" to define the function after I
have declared the function by "f(void)"? Or I can simply use "f()" to
define the function?

Best wishes,
Peng

Nov 15 '05 #1
16 1521


Pe*******@gmail.com wrote:
Hi,

I find I have to declare a function like "int f(void);", if the
function should not accept any arguments. If I declare with "int f();"
instead, the compiler won't give any error if I call the function f
with arguments.

I'm wondering when I should use "f()" instead of "f(void)" to declare
the function. Should I use "f(void)" to define the function after I
have declared the function by "f(void)"? Or I can simply use "f()" to
define the function?


You *can* use plain f() in the function definition, but
it's not a good idea. It's a hold-over from the pre-Standard
days, and is described in the Standard as an "obsolescent
feature." Best practice: declare f(void) and define f(void).

--
Er*********@sun.com

Nov 15 '05 #2

<Pe*******@gmail.com> wrote

I'm wondering when I should use "f()" instead of "f(void)"

Basically never. f() is for playing silly games with C's calling system
which hackers do for fun and experienced programmers very occasionally might
have to do to get round some inherent weakness in the language.
Nov 15 '05 #3
On Wed, 22 Jun 2005 18:25:18 -0400, Eric Sosman <er*********@sun.com>
wrote:


Pe*******@gmail.com wrote:
Hi,

I find I have to declare a function like "int f(void);", if the
function should not accept any arguments. If I declare with "int f();"
instead, the compiler won't give any error if I call the function f
with arguments.

I'm wondering when I should use "f()" instead of "f(void)" to declare
the function. Should I use "f(void)" to define the function after I
have declared the function by "f(void)"? Or I can simply use "f()" to
define the function?


You *can* use plain f() in the function definition, but
it's not a good idea. It's a hold-over from the pre-Standard
days, and is described in the Standard as an "obsolescent
feature." Best practice: declare f(void) and define f(void).


Besides, it's easier. For me, at least - I have an editor macro which
copies the definition and tucks it away in a selected spot, followed
by ';'.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #4
Hi Peng and others,
I thought that f(void) was meant to make it very explicit that function
f() does not receive any arguments.

Nov 15 '05 #5
Rajan wrote:
Hi Peng and others,
I thought that f(void) was meant f(void)
is incomplete - it tells you about a function f that takes *no*
parameters
and nothing else about the return type. to make it very explicit that function I beg to differ, here. f() does not receive any arguments.

f()
is even more incomplete - it tells you about a function f and nothing
else, neither the return type nor the parameter list (it is
unspecified, and hence can be whatever you chose it to be).

Regards,
Suman.

Nov 15 '05 #6
Rajan wrote:
Hi Peng and others,
I thought that f(void) was meant f(void)
is incomplete - it tells you about a function f that takes *no*
parameters
and nothing else about the return type. to make it very explicit that function I beg to differ, here. f() does not receive any arguments.

f()
is even more incomplete - it tells you about a function f and nothing
else, neither the return type nor the parameter list (it is
unspecified, and hence can be whatever you chose it to be).

Regards,
Suman.

Nov 15 '05 #7
If you declare the function like "int f();" the compiler won't give any
error, because by default it will be integer if you don't mention any
data type.So you can pass any integer argument to the function, not
any other data type.

Nov 15 '05 #8


Pe*******@gmail.com wrote:
Hi,

I find I have to declare a function like "int f(void);", if the
function should not accept any arguments. If I declare with "int f();"
instead, the compiler won't give any error if I call the function f
with arguments.

That's because the declaration "int f();" says f takes an
*indeterminate* number of parameters. This is a holdover from the old
K&R days when there was no compile-time checking for the right number
and types of parameters in a function call. With the introduction of
prototypes, this form of function declaration has become obsolete and
should not be used anymore, even though compilers still support it.
I'm wondering when I should use "f()" instead of "f(void)" to declare
the function.
In C, never[1]. If the function is supposed to take no arguments, you
should always specify that explicitly with "void" as the parameter
list.
Should I use "f(void)" to define the function after I
have declared the function by "f(void)"? Or I can simply use "f()" to
define the function?

Declarations and definitions should always match. If you declare the
function as "int f(void);", the definition should be "int f(void) {
/*code*/ }".
Best wishes,
Peng


1. Note that in C++, "int f();" is the same as "int f(void);" in C.

Nov 15 '05 #9
Joel wrote:
If you declare the function like "int f();" the compiler won't give any
error, because by default it will be integer if you don't mention any
data type.So you can pass any integer argument to the function, not
any other data type.


Guys, please correct me if I am wrong, but given my C++ background, I
think what Joel is trying to say is "int foo()" and "foo()" are same in
that both have _return_ type int! Joel, I don't think either
declaration can take an argument.

cheers,
forayer

Nov 15 '05 #10
Suman wrote:
Rajan wrote:
Hi Peng and others,
I thought that f(void) was meant

f(void)
is incomplete - it tells you about a function f that takes *no*
parameters
and nothing else about the return type.


AFAIK, it implicitly returns an int (in C89). See for example the
programs in K&R2 using "main()".
ishs

Nov 15 '05 #11
On Thu, 23 Jun 2005 08:01:36 -0700, forayer wrote:
Joel wrote:
If you declare the function like "int f();" the compiler won't give any
error, because by default it will be integer if you don't mention any
data type.So you can pass any integer argument to the function, not
any other data type.

The argument type is not limited to int.
Guys, please correct me if I am wrong, but given my C++ background, I
think what Joel is trying to say is "int foo()" and "foo()" are same in
that both have _return_ type int! Joel, I don't think either
declaration can take an argument.


Joel was talking about the arguments, not the return value. This is a case
where C and C++ differ. When you give a completely empty argument list in
a C declaration that isn't a definition, it doesn't mean no arguments it
means unspecified arguments. C99 no longer supports implicit int so just
"foo()" is invalid.

Lawrence
Nov 15 '05 #12


ade ishs wrote:
Suman wrote:
Rajan wrote:
Hi Peng and others,
I thought that f(void) was meant

f(void)
is incomplete - it tells you about a function f that takes *no*
parameters
and nothing else about the return type.


AFAIK, it implicitly returns an int (in C89). See for example the
programs in K&R2 using "main()".

Unfortunately, yes. But from C99, no - it doesn't.

Regards,
Suman.

Nov 15 '05 #13
Pe*******@gmail.com wrote:
Hi,

I find I have to declare a function like "int f(void);", if the
function should not accept any arguments. If I declare with "int f();"
instead, the compiler won't give any error if I call the function f
with arguments.

I'm wondering when I should use "f()" instead of "f(void)" to declare
the function. Should I use "f(void)" to define the function after I
have declared the function by "f(void)"? Or I can simply use "f()" to
define the function?

Best wishes,
Peng


I would suggest you use int f(void) at both places that is decl. and at
definition.

int f() could mean that your function **may** accept variable number of
arguments. Soo, to tell compiler and others that your function does not
accept any parameters, gor for int f(void).

Let me know if I am wrong.

Nov 15 '05 #14
> That's because the declaration "int f();" says f takes an
*indeterminate* number of parameters. This is a holdover from the old
K&R days when there was no compile-time checking for the right number
and types of parameters in a function call. With the introduction of
prototypes, this form of function declaration has become obsolete and
should not be used anymore, even though compilers still support it.


Could you tell me more about "prototypes"?

Nov 15 '05 #15


Pe*******@gmail.com wrote:
That's because the declaration "int f();" says f takes an
*indeterminate* number of parameters. This is a holdover from the old
K&R days when there was no compile-time checking for the right number
and types of parameters in a function call. With the introduction of
prototypes, this form of function declaration has become obsolete and
should not be used anymore, even though compilers still support it.


Could you tell me more about "prototypes"?


You'd be better off checking your favorite C reference manual; it will
give a better explanation than I can.

Basically, a function can be declared or defined using a prototype
syntax, meaning that you specify the types of arguments as part of the
parameter list. The best way I can explain is by using examples:

/*
** Function definition using old style, non-prototype syntax (it's been
a
** while since I've written C code using this style, so I may not have
this
** exactly right, but you get the general idea).
*/
int f(x, y, z)
int x;
double y;
char *z;
{
/* code */
}

/*
** Same function definition using prototype syntax
*/
int f(int x, double y, char *z)
{
/* code */
}

/*
** Function declaration using non-prototype syntax
*/
int f();

/*
** Same function declaration using prototype syntax
*/
int f(int x, double y, char *z);
/*
** Function taking no arguments, non-prototype syntax
*/
void f(); /* declaration */

void f() /* definition */
{
/* code */
}

/*
** Same function, using prototype syntax
*/
void f(void); /* declaration */

void f(void) /* definition */
{
/* code */
}

Basically, the presence of a prototype allows the compiler to verify
that you're calling a function with the right number and types of
parameters. You should *always* use prototypes.

Nov 15 '05 #16
On 23 Jun 2005 21:26:27 -0700, "Jaspreet" <js***********@gmail.com>
wrote:
Pe*******@gmail.com wrote: <snip prototype int f(void) vs oldstyle int f()> I would suggest you use int f(void) at both places that is decl. and at
definition.

int f() could mean that your function **may** accept variable number of
arguments. Soo, to tell compiler and others that your function does not
accept any parameters, gor for int f(void).

Let me know if I am wrong.


Officially you are. int f() means the function accepts an unspecified
but fixed number (and list) of arguments of K&R1-compatible types,
which are those which survive after default promotion = excluding
integer types below int and float.

It is Undefined Behavior to call a variadic (aka varargs) function
without using a prototype declaration specifying ellipsis, so they can
have a different calling convention than "normal" functions including
K&R1-compatible ones, and I have used an implementation which does so
and crashes badly if you confuse them, although _many_ implementations
including all-in-memory register-poor x86 do use a calling convention
which works the same for variadic and non.

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #17

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

Similar topics

1
1857
by: Capstar | last post by:
Hi NG, I have a question on std::for_each. I try to use this in combination with std::bind2nd to call a method of all functions in a container (std::set or std::map) and pass that method the...
2
2519
by: j0mbolar | last post by:
given this example: void bar(const char *fmt, ...) { va_list ap; va_start(ap, fmt); baz(fmt, ap); va_end(ap);
48
2672
by: Michael Sig Birkmose | last post by:
Hi everyone! Does anyone know, if it is possible to meassure the maximum stack usage of a C program throughout it's entire execution? -- Michael Birkmose
188
17200
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
5280
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
12
3133
by: Ian | last post by:
I read the FAQ about the differences between memcpy() and memmove(). Apparently memmove() is supposed to be safer. Just to make sure I understand the concept of memmove(), can someone tell me if...
2
1674
by: hn.ft.pris | last post by:
I've got following code test C++'s functor. For the sake of easy-reading, I omit some declearations. #include <algorithm> #include <functional> using namespace std;
9
2820
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
47
2012
by: Tak | last post by:
Hi : I want to know how to use %p in the program.Help me! Does it mean %x?
0
7237
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
7137
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
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.