473,624 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2089
In <15************ **************@ posting.google. com> mi*****@mailcit y.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*****@mailcit y.com (Razvan) wrote in message news:<15******* *************** ****@posting.go ogle.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.c om (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.c h> wrote:
In <a5************ **************@ posting.google. com> k_*****@yahoo.c om (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.c h> wrote:
In <a5************ **************@ posting.google. com> k_*****@yahoo.c om (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.c h> 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.c h> 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.c h> 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.l earn.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

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

Similar topics

220
18988
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
699
33834
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
226
12494
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
92
6431
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? cheers, reed
125
14685
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
10
54866
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 idea! function closeall() { var objs;
21
13816
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 by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
669
25819
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
44
2915
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 class is used as a function that keeps state from one call to the next. The problem is that I don't know what to call such a thing! "Abstract class" isn't right, because that implies that you should subclass the class and then instantiate the...
0
8233
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.