473,320 Members | 1,848 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,320 software developers and data experts.

What is the purpose of such a function ?

Hi!

What is the purpose of such a function ?
int function(void)
{

return 1;
}

It seems to be completely equivalent with
int function()
{

return 1;
}

Is its behaviour different on a C versus a C++ compiler ?

Regards,
Razvan
Nov 14 '05 #1
18 2055
In <15**************************@posting.google.com > mi*****@mailcity.com (Razvan) writes:
What is the purpose of such a function ?

int function(void)
{
return 1;
}

It seems to be completely equivalent with

int function()
{
return 1;
}

Is its behaviour different on a C versus a C++ compiler ?


Yes. On a C compiler, the latter is an old style function definition,
that doesn't provide a prototype for the function:

fangorn:~/tmp 183> cat test.c
int function() { return 0; }

int main() { return function(2, 3); }
fangorn:~/tmp 184> gcc test.c
fangorn:~/tmp 185> g++ test.c
test.c: In function `int main()':
test.c:1: error: too many arguments to function `int function()'
test.c:3: error: at this point in file

The original version has identical semantics under both C and C++.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #2
kal
mi*****@mailcity.com (Razvan) wrote in message news:<15**************************@posting.google. com>...
What is the purpose of such a function ?

int function(void)
{

return 1;
}

It seems to be completely equivalent with

int function()
{

return 1;
}

Is its behaviour different on a C versus a C++ compiler ?


I think so.

In C: "int f(void);" delcares a function with NO parameters
returning an int. "int f();" declares a function with no
parameter SPECIFICATION returning an int.
Nov 14 '05 #3
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
In C: "int f(void);" delcares a function with NO parameters
returning an int. "int f();" declares a function with no
parameter SPECIFICATION returning an int.


Well, it still specifies something: that the unknown number of parameters
of unknown types is fixed, i.e. f() cannot be a variadic function.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
Dan Pop <Da*****@cern.ch> wrote:
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
In C: "int f(void);" delcares a function with NO parameters
returning an int. "int f();" declares a function with no
parameter SPECIFICATION returning an int.

Well, it still specifies something: that the unknown number of parameters
of unknown types is fixed, i.e. f() cannot be a variadic function.


It seems (by experiment) that you can make both f(2,3) and f(2,3,4)
calls in the same function main().
Then is it possible (or was it in the "old style") to define:
int f(int n, ...) { /*...*/ }
and declare in the header:
int f();
?

--
Stan Tobias
Nov 14 '05 #5
In <2j*************@uni-berlin.de> "S.Tobias" <sN*******@amu.edu.pl> writes:
Dan Pop <Da*****@cern.ch> wrote:
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
>In C: "int f(void);" delcares a function with NO parameters
>returning an int. "int f();" declares a function with no
>parameter SPECIFICATION returning an int.

Well, it still specifies something: that the unknown number of parameters
of unknown types is fixed, i.e. f() cannot be a variadic function.


It seems (by experiment) that you can make both f(2,3) and f(2,3,4)
calls in the same function main().


At least one of them invokes undefined behaviour.
Then is it possible (or was it in the "old style") to define:
int f(int n, ...) { /*...*/ }
and declare in the header:
int f();
?


The ellipsis thing didn't exist at all pre-ANSI. On implementations
supporting the <varargs.h> interface, you'd declare it as

int f();

and define it like this:

#include <varargs.h>

int f(va_alist)
va_dcl /* no semicolon */
{
...
}

The actual mechanism for accessing the parameter list was relatively
similar to the one provided by <stdarg.h>.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Dan Pop <Da*****@cern.ch> wrote:
of unknown types is fixed, i.e. f() cannot be a variadic function.

Could you, please, give me the place in the Standard that supports this?

I'm not challenging your claim at all. I've checked on-line como
and it refuses to take
int f();
int f(int i, ...) { return 0; }
both in C90 and C99 mode.
But OTOH I've searched through the Standard and the only explicit
statement about this I could find is a note in 7.15.1.4#6 in an
example for va_start macro:
[...]
void f1(int n_ptrs, ...)
{
[...]
}
Each call to f1 is required to have visible the definition of the
function or a declaration such as
void f1(int, ...);
But AFAIK examples are not normative.
It seems (by experiment) that you can make both f(2,3) and f(2,3,4)
calls in the same function main().
At least one of them invokes undefined behaviour.
What I meant is that gcc didn't protest. I thought that the compiler
might be wise enough to notice different number of arguments, if it
were an issue. Since it didn't (neither did como btw), I thought it
might consider f() variadic, which led me to next question.
Then is it possible (or was it in the "old style") to define:
int f(int n, ...) { /*...*/ }
and declare in the header:
int f();
?

The ellipsis thing didn't exist at all pre-ANSI. On implementations
supporting the <varargs.h> interface, you'd declare it as

[snip]

Thank you!

--
Stan Tobias
Nov 14 '05 #7
"S.Tobias" <sN*******@amu.edu.pl> wrote:
Dan Pop <Da*****@cern.ch> wrote:
> of unknown types is fixed, i.e. f() cannot be a variadic function.


Could you, please, give me the place in the Standard that supports this?


What about 6.5.2.2 "Function Calls", paragraph 6?

"If the expression that denotes the called function has a type that
does not include a prototype, the integer promotions are performed
on each argument, and arguments that have type float are promoted
to double. These are called the default argument promotions. If the
number of arguments does not equal the number of parameters, the
behavior is undefined. If the function is defined with a type that
includes a prototype, and either the prototype ends with an
ellipsis (, ...) or the types of the arguments after promotion are
not compatible with the types of the parameters, the behavior is
undefined."

This paragraph makes it undefined behaviour to call a variadic function
through an unprototyped declaration.

--
Simon.
Nov 14 '05 #8
On 22 Jun 2004 21:22:55 GMT, "S.Tobias" <sN*******@amu.edu.pl> wrote
in comp.lang.c:
Dan Pop <Da*****@cern.ch> wrote:
> of unknown types is fixed, i.e. f() cannot be a variadic function.
Could you, please, give me the place in the Standard that supports this?
It is not easy to parse it out, but it is in C99 6.5.2.2, P6 which
talks about calls to functions that do not have a prototype (although
in C99 they must have at least a declaration):

[begin quote]

If the expression that denotes the called function has a type that
does not include a prototype, the integer promotions are performed on
each argument, and arguments that have type float are promoted to
double. These are called the default argument promotions. If the
number of arguments does not equal the number of parameters, the
behavior is undefined. If the function is defined with a type that
includes a prototype, and either the prototype ends with an ellipsis
(, ...) or the types of the arguments after promotion are not
compatible with the types of the parameters, the behavior is
undefined.

[end quote]

So if you call a function without a prototype in scope and it is
variadic (i.e., it is defined with an argument list ending in ", ...",
the behavior is specifically undefined.

I'm not challenging your claim at all. I've checked on-line como
and it refuses to take
int f();
int f(int i, ...) { return 0; }
both in C90 and C99 mode.
But OTOH I've searched through the Standard and the only explicit
statement about this I could find is a note in 7.15.1.4#6 in an
example for va_start macro:
[...]
void f1(int n_ptrs, ...)
{
[...]
}
Each call to f1 is required to have visible the definition of the
function or a declaration such as
void f1(int, ...);
But AFAIK examples are not normative.
It seems (by experiment) that you can make both f(2,3) and f(2,3,4)
calls in the same function main().

At least one of them invokes undefined behaviour.


What I meant is that gcc didn't protest. I thought that the compiler
might be wise enough to notice different number of arguments, if it
were an issue. Since it didn't (neither did como btw), I thought it
might consider f() variadic, which led me to next question.


Remember, the standard places no requirements at all in cases of
undefined behavior. An implementation is allowed, but not required,
to emit a diagnostic for undefined behavior. But it is also allowed,
but not required, to emit a diagnostic for any reason at all.

i.e.:

# ds2kc welldefined.c
Death Station 2000 C2009 Compiler Version 1.0
compiling welldefined.c...
Lovely weather we're having today, isn't it?
0 errors
#

The wording in C89/90 was similar, although it had text allowing for
implicit declaration of functions returning int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
> >What is the purpose of such a function ?

int function(void)
{
return 1;
}

It seems to be completely equivalent with

int function()
{
return 1;
}

Is its behaviour different on a C versus a C++ compiler ?


Yes. On a C compiler, the latter is an old style function definition,
that doesn't provide a prototype for the function:

fangorn:~/tmp 183> cat test.c
int function() { return 0; }

int main() { return function(2, 3); }
fangorn:~/tmp 184> gcc test.c
fangorn:~/tmp 185> g++ test.c
test.c: In function `int main()':
test.c:1: error: too many arguments to function `int function()'
test.c:3: error: at this point in file

The original version has identical semantics under both C and C++.

You are able to compile the code with the C compiler (gcc) but
not with the C++ compiler (g++).

That means:
1. int f(void)
Under both C and C++ languages it is a function that takes no
parameters and return an int;

2. int f()
in C - a function that takes ANY number of parameters;
in C++ - a function that takes NO parameters.

So, if you want to write a function that takes no parameters
that is valid C & C++ code you should write:

int f(void)

Thanks,
Razvan
Nov 14 '05 #10
Jack Klein <ja*******@spamcop.net> wrote:
On 22 Jun 2004 21:22:55 GMT, "S.Tobias" <sN*******@amu.edu.pl> wrote
in comp.lang.c:
Dan Pop <Da*****@cern.ch> wrote:
>> of unknown types is fixed, i.e. f() cannot be a variadic function. Could you, please, give me the place in the Standard that supports this?

It is not easy to parse it out, but it is in C99 6.5.2.2, P6 which
talks about calls to functions that do not have a prototype (although
in C99 they must have at least a declaration): [begin quote] If the expression that denotes the called function has a type that
does not include a prototype, the integer promotions are performed on
each argument, and arguments that have type float are promoted to
double. These are called the default argument promotions. If the
number of arguments does not equal the number of parameters, the
behavior is undefined. If the function is defined with a type that
includes a prototype, and either the prototype ends with an ellipsis
(, ...) or the types of the arguments after promotion are not
compatible with the types of the parameters, the behavior is
undefined. [end quote] So if you call a function without a prototype in scope and it is
variadic (i.e., it is defined with an argument list ending in ", ...",
the behavior is specifically undefined.
Ah, yes! I had to read it a few times to understand it correctly!

It's still worth adding that you should not define a variadic function
without a prototype: "If a function that accepts a variable number
of arguments is defined without a parameter type list that ends
with the ellipsis notation, the behavior is undefined." (6.9.1#8).

Thanks a lot!

# ds2kc welldefined.c
Death Station 2000 C2009 Compiler Version 1.0
compiling welldefined.c...
Lovely weather we're having today, isn't it?
0 errors
#


:-)

--
Stan Tobias
Nov 14 '05 #11
In <2j*************@uni-berlin.de> "S.Tobias" <sN*******@amu.edu.pl> writes:
Dan Pop <Da*****@cern.ch> wrote:
>> of unknown types is fixed, i.e. f() cannot be a variadic function.
Could you, please, give me the place in the Standard that supports this?
Since I have already located it for another thread, it's quite easy.
I'm not challenging your claim at all. I've checked on-line como
and it refuses to take
int f();
int f(int i, ...) { return 0; }
both in C90 and C99 mode.


It is right, as f is not declared with compatible types in the two
declarations:

6.7.5.3

15 For two function types to be compatible, both shall specify
compatible return types. Moreover, the parameter type lists,
if both are present, shall agree in the number of parameters and
in use of the ellipsis terminator; corresponding parameters shall
have compatible types. If one type has a parameter type list
and the other type is specified by a function declarator that
is not part of a function definition and that contains an empty
identifier list, the parameter list shall not have an ellipsis
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
terminator and the type of each parameter shall be compatible
^^^^^^^^^^
with the type that results from the application of the default
argument promotions...

Contrary to what other people said, this is not a case of undefined
behaviour: a diagnostic is required, because there are two conflicting
declarations for the same identifier, at file scope.

You'd have undefined behaviour if the declaration and definition were
in different translation units. Typical example:

int printf();

int main()
{
printf("hello world\n");
return 0;
}
>It seems (by experiment) that you can make both f(2,3) and f(2,3,4)
>calls in the same function main().

At least one of them invokes undefined behaviour.


What I meant is that gcc didn't protest. I thought that the compiler
might be wise enough to notice different number of arguments, if it
were an issue. Since it didn't (neither did como btw), I thought it
might consider f() variadic, which led me to next question.


Undefined behaviour doesn't require a diagnostic. The compiler didn't
have a prototype for f() in scope, so it had nothing to check the function
calls against. What it did see is that f(2, 3) is compatible with the
declaration of f() and that f(2, 3, 4) is compatible with the declaration
of f(), so there was nothing to complain about. No attempt to check
whether the two function calls were consistent.

More sophisticated code checkers would build a prototype from the
first call of f(): int f(int, int) and check all other calls of f()
against this prototype. Allowed, but not required by the standard.

This was quite popular in the pre-ANSI days, when prototypes didn't
exist at all. The down side of this technique was that it didn't
handle the unrecognised variadic functions calls well (they were
declared like ordinary functions, back then), so you had to somehow
inform the tool that it is dealing with a variadic function.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
On Tue, 22 Jun 2004 17:08:37 +0000, Dan Pop wrote:
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
In C: "int f(void);" delcares a function with NO parameters
returning an int. "int f();" declares a function with no
parameter SPECIFICATION returning an int.


Well, it still specifies something: that the unknown number of parameters
of unknown types is fixed, i.e. f() cannot be a variadic function.

int f(); declares function that can be called with any number of
arguments with any type.

Nov 14 '05 #13
Nils O. Selåsdal <NO*@Utel.no> wrote in message news:<pa****************************@Utel.no>...
On Tue, 22 Jun 2004 17:08:37 +0000, Dan Pop wrote:
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
In C: "int f(void);" delcares a function with NO parameters
returning an int. "int f();" declares a function with no
parameter SPECIFICATION returning an int.


Well, it still specifies something: that the unknown number of parameters
of unknown types is fixed, i.e. f() cannot be a variadic function.


int f(); declares function that can be called with any number of
arguments with any type.


No. It declares a function with a fixed number of parameters of
fixed types, but it doesn't tell you what the number or types are.
Nov 14 '05 #14
In <pa****************************@Utel.no> =?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?= <NO*@Utel.no> writes:
On Tue, 22 Jun 2004 17:08:37 +0000, Dan Pop wrote:
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
In C: "int f(void);" delcares a function with NO parameters
returning an int. "int f();" declares a function with no
parameter SPECIFICATION returning an int.
Well, it still specifies something: that the unknown number of parameters
of unknown types is fixed, i.e. f() cannot be a variadic function.

int f(); declares function that can be called with any number of
arguments with any type.


If that were true, f(1), f(1, 2) and f(1, 2, 3) could ALL be correct calls
of f(). In fact, at most one of them can be correct. For all of them to
be correct, f has to be declared like this:

int f(int, ...);

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15
Dan Pop wrote:
<NO*@Utel.no> writes:
.... snip ...
int f(); declares function that can be called with any number of
arguments with any type.


If that were true, f(1), f(1, 2) and f(1, 2, 3) could ALL be
correct calls of f(). In fact, at most one of them can be correct.
For all of them to be correct, f has to be declared like this:

int f(int, ...);


I have a slight problem seeing how that first argument of 1 can be
used at the function end to discriminate between the three calls
:-) Now if you had suggested f(1), f(2,1), f(3,2,1) as the
possible calls, I could be convinced.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #16
CBFalconer wrote:
Dan Pop wrote:
<NO*@Utel.no> writes:


... snip ...
int f(); declares function that can be called with any number of
arguments with any type.


If that were true, f(1), f(1, 2) and f(1, 2, 3) could ALL be
correct calls of f(). In fact, at most one of them can be correct.
For all of them to be correct, f has to be declared like this:

int f(int, ...);

I have a slight problem seeing how that first argument of 1 can be
used at the function end to discriminate between the three calls
:-) Now if you had suggested f(1), f(2,1), f(3,2,1) as the
possible calls, I could be convinced.


int how_many_f_args;
int f(int arg1, ...) {
int i;
va_list ap;
printf ("Arg 1 = %d\n", arg1);
va_start (ap, arg1);
for (i = 2; i <= how_many_f_args; ++i)
printf ("Arg %d = %d\n", i, va_arg(ap, int));
va_end (ap);
return 42;
}
...
how_many_f_args = 1; f(1);
how_many_f_args = 2; f(1, 2);
how_many_f_args = 3; f(1, 2, 3);

Bad practice, I'd say, but certainly possible.

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

Nov 14 '05 #17
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Dan Pop wrote:
<NO*@Utel.no> writes:

... snip ...
int f(); declares function that can be called with any number of
arguments with any type.


If that were true, f(1), f(1, 2) and f(1, 2, 3) could ALL be
correct calls of f(). In fact, at most one of them can be correct.
For all of them to be correct, f has to be declared like this:

int f(int, ...);


I have a slight problem seeing how that first argument of 1 can be
used at the function end to discriminate between the three calls
:-) Now if you had suggested f(1), f(2,1), f(3,2,1) as the
possible calls, I could be convinced.


The information about the number of arguments need not be passed as an
explicit function argument. I'll let you figure out other ways of
conveying it...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #18
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:
CBFalconer wrote:
Dan Pop wrote:
<NO*@Utel.no> writes:


... snip ...
int f(); declares function that can be called with any number of
arguments with any type.

If that were true, f(1), f(1, 2) and f(1, 2, 3) could ALL be
correct calls of f(). In fact, at most one of them can be correct.
For all of them to be correct, f has to be declared like this:

int f(int, ...);

I have a slight problem seeing how that first argument of 1 can be
used at the function end to discriminate between the three calls
:-) Now if you had suggested f(1), f(2,1), f(3,2,1) as the
possible calls, I could be convinced.


int how_many_f_args;
int f(int arg1, ...) {
int i;
va_list ap;
printf ("Arg 1 = %d\n", arg1);
va_start (ap, arg1);
for (i = 2; i <= how_many_f_args; ++i)
printf ("Arg %d = %d\n", i, va_arg(ap, int));
va_end (ap);
return 42;
}
...
how_many_f_args = 1; f(1);
how_many_f_args = 2; f(1, 2);
how_many_f_args = 3; f(1, 2, 3);

Bad practice, I'd say, but certainly possible.


There are more interesting ways, based on conventions between the two
parties. Two that would make my example work are:

1. The first invocation of f() takes one argument, the second two and so
on. f() has a static invocation counter that is also its return value.

2. The last argument of f() specifies the length of the variable part
of the *next* call of f(). The first call has no variable part.
The function returns the number of parameters it was expecting
when invoked.

Of course, I'm not recommending them to anyone, merely pointing out ways
of making my arbitrary example work.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #19

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
10
by: InvisibleMan | last post by:
Hi, Thanks for any help in advance... Okay, I have the JS listed below that calls for the display of the (DIV) tag... cookie function not included, as don't feel its necessary but you'll get the...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.