Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   function call with arguments which takes no arguments (http://www.velocityreviews.com/forums/t436554-function-call-with-arguments-which-takes-no-arguments.html)

Neo 01-19-2005 10:27 AM

function call with arguments which takes no arguments
 
Why the following code is compilable? The function abc() doesn't take any
arguments, still i can call the function with arbitraty number of arguments.
Even compiler doesn't show any warning. What the standard says?

----- file1.c ------
extern unsigned abc();
int main()
{
unsigned *chip_offset;
abc(&chip_offset, 10);
/* do something with pointer - which is errornous */
return 0;
}

----- file2.c -----
unsigned abc()
{
unsigned some_address;
some_address = 0xFFBA;
return some_address;
}


-Neo
"If you don't program yourself, life will program you!"



S.Tobias 01-19-2005 10:45 AM

Re: function call with arguments which takes no arguments
 
Neo <timeless_illusion@yahoo.com> wrote:

> Why the following code is compilable? The function abc() doesn't take any
> arguments, still i can call the function with arbitraty number of arguments.
> Even compiler doesn't show any warning. What the standard says?


UB

(N869)
6.5.2.2 Function calls
[#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 agree with the number of parameters, the
behavior is undefined. If the function is defined with a

> ----- file1.c ------
> extern unsigned abc();


Here above you declare abc to be a function with unspecified number
of parameters (ie. function without a prototype). The compiler
does not assume anything about the number of arguments (except
that it is constant). (note: this is unlike in C++.)

This is the declaration of abc as a function with no parameters:

extern unsigned abc(void);

> int main()
> {
> unsigned *chip_offset;
> abc(&chip_offset, 10);
> /* do something with pointer - which is errornous */
> return 0;
> }


> ----- file2.c -----
> unsigned abc()
> {
> unsigned some_address;
> some_address = 0xFFBA;
> return some_address;
> }



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

aegis 01-19-2005 10:57 AM

Re: function call with arguments which takes no arguments
 

S.Tobias wrote:
> Neo <timeless_illusion@yahoo.com> wrote:
>
> > Why the following code is compilable? The function abc() doesn't

take any
> > arguments, still i can call the function with arbitraty number of

arguments.
> > Even compiler doesn't show any warning. What the standard says?

>
> UB
>
> (N869)
> 6.5.2.2 Function calls
> [#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 agree with the number of parameters, the
> behavior is undefined. If the function is defined with a
>


I don't think this applies here. He has a function declarator
that is not part of a definition, which signals to the
compiler that there is no information given as to the number of
arguments to the function. So the use seems to be perfectly okay
aside from the fact that it is considered obsolescent.

> > ----- file1.c ------
> > extern unsigned abc();

>
> Here above you declare abc to be a function with unspecified number
> of parameters (ie. function without a prototype). The compiler
> does not assume anything about the number of arguments (except
> that it is constant). (note: this is unlike in C++.)
>
> This is the declaration of abc as a function with no parameters:
>
> extern unsigned abc(void);
>
> > int main()
> > {
> > unsigned *chip_offset;
> > abc(&chip_offset, 10);
> > /* do something with pointer - which is errornous */
> > return 0;
> > }

>
> > ----- file2.c -----
> > unsigned abc()
> > {
> > unsigned some_address;
> > some_address = 0xFFBA;
> > return some_address;
> > }

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

--
aegis


Keith Thompson 01-19-2005 05:24 PM

Re: function call with arguments which takes no arguments
 
"aegis" <aegis@mad.scientist.com> writes:
> S.Tobias wrote:
>> Neo <timeless_illusion@yahoo.com> wrote:
>>
>> > Why the following code is compilable? The function abc() doesn't

> take any
>> > arguments, still i can call the function with arbitraty number of

> arguments.
>> > Even compiler doesn't show any warning. What the standard says?

>>
>> UB
>>
>> (N869)
>> 6.5.2.2 Function calls
>> [#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 agree with the number of parameters, the
>> behavior is undefined. If the function is defined with a
>>

>
> I don't think this applies here. He has a function declarator
> that is not part of a definition, which signals to the
> compiler that there is no information given as to the number of
> arguments to the function. So the use seems to be perfectly okay
> aside from the fact that it is considered obsolescent.


The "extern" declaration of abc() says it takes an unspecified number
of arguments. The *definition* of abc() in file2.c says it takes no
arguments. The call passes two arguments. Undefined behavior.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Mike Wahler 01-19-2005 11:02 PM

Re: function call with arguments which takes no arguments
 

"Neo" <timeless_illusion@yahoo.com> wrote in message
news:356r0oF4j27dbU1@individual.net...
> Why the following code is compilable? The function abc() doesn't take any
> arguments, still i can call the function with arbitraty number of

arguments.
> Even compiler doesn't show any warning. What the standard says?
>
> ----- file1.c ------
> extern unsigned abc();


This prototype does not indicate 'no arguments'.
It indicates 'arguments not specified'. It's
also a fragile way to do things.

-Mike




Mike Wahler 01-20-2005 01:32 AM

Re: function call with arguments which takes no arguments
 

"Peter Nilsson" <airia@acay.com.au> wrote in message
news:1106177977.669031.53940@z14g2000cwz.googlegro ups.com...
> Mike Wahler wrote:
> > "Neo" <timeless_illusion@yahoo.com> wrote...
> > > Why the following code is compilable? The function abc() doesn't
> > > take any arguments, still i can call the function with arbitraty
> > > number of arguments.
> > > Even compiler doesn't show any warning. What the standard says?
> > >
> > > ----- file1.c ------
> > > extern unsigned abc();

> >
> > This prototype does not indicate 'no arguments'.

>
> It's not a prototype (in C.) It's only a function declaration.


You're right, I should have written 'declaration'.

-Mike



Neo 01-20-2005 03:15 AM

Re: function call with arguments which takes no arguments
 

"Peter Nilsson" <airia@acay.com.au> wrote in message
news:1106177977.669031.53940@z14g2000cwz.googlegro ups.com...
> Mike Wahler wrote:
>> "Neo" <timeless_illusion@yahoo.com> wrote...
>> > Why the following code is compilable? The function abc() doesn't
>> > take any arguments, still i can call the function with arbitraty
>> > number of arguments.
>> > Even compiler doesn't show any warning. What the standard says?
>> >
>> > ----- file1.c ------
>> > extern unsigned abc();

>>
>> This prototype does not indicate 'no arguments'.

>
> It's not a prototype (in C.) It's only a function declaration.


What is the difference between a function declaration and a function
prototype?

>
>> It indicates 'arguments not specified'. It's
>> also a fragile way to do things.

>
> --
> Peter
>


-Neo



Chris Torek 01-20-2005 05:02 AM

Re: function call with arguments which takes no arguments
 
In article <358n3sF4j7ei1U1@individual.net>
Neo <timeless_illusion@yahoo.com> wrote:
>What is the difference between a function declaration and a function
>prototype?


To be a prototype, you have to have at least one type-name inside
the parentheses:

int zorg(); /* declaration but not prototype */
int zorg(void); /* declaration and prototype */

The empty parentheses syntax exists for compatibility with pre-1989
C code. It is best not to use it in any code written since 1989,
because prototypes supply more information, so that compilers can
check the actual calls to the functions.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Jack Klein 01-20-2005 05:14 AM

Re: function call with arguments which takes no arguments
 
On Thu, 20 Jan 2005 08:45:59 +0530, "Neo"
<timeless_illusion@yahoo.com> wrote in comp.lang.c:

>
> "Peter Nilsson" <airia@acay.com.au> wrote in message
> news:1106177977.669031.53940@z14g2000cwz.googlegro ups.com...
> > Mike Wahler wrote:
> >> "Neo" <timeless_illusion@yahoo.com> wrote...
> >> > Why the following code is compilable? The function abc() doesn't
> >> > take any arguments, still i can call the function with arbitraty
> >> > number of arguments.
> >> > Even compiler doesn't show any warning. What the standard says?
> >> >
> >> > ----- file1.c ------
> >> > extern unsigned abc();
> >>
> >> This prototype does not indicate 'no arguments'.

> >
> > It's not a prototype (in C.) It's only a function declaration.

>
> What is the difference between a function declaration and a function
> prototype?
>
> >
> >> It indicates 'arguments not specified'. It's
> >> also a fragile way to do things.

> >
> > --
> > Peter
> >

>
> -Neo


I already posted an extensive answer to your original post in
comp.arch.embedded.

In the future, if you are going to post the same article to multiple
groups, after making sure that it is topical in the multiple groups,
cross-post it, that is put all the newsgroup names in a single post.

As to this question, a function prototype specifies the name of a
function, the type it returns, and the number and types of arguments:

int fred(double ricky, char *ethel);

....is a prototype, as is:

int fred(double, char *);

....because names for the arguments are optional except when you define
the body of a function.

Every line that specifies a function name, return type, and a pair of
parentheses is a function declaration. If it also includes the types
of the parameters, it is a prototype.

--
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

Neo 01-20-2005 05:43 AM

Re: function call with arguments which takes no arguments
 

"Jack Klein" <jackklein@spamcop.net> wrote in message
news:3afuu0d1n1hcrk0j8tptsf2ienr823fr04@4ax.com...
> On Thu, 20 Jan 2005 08:45:59 +0530, "Neo"
> <timeless_illusion@yahoo.com> wrote in comp.lang.c:
>
>>
>> "Peter Nilsson" <airia@acay.com.au> wrote in message
>> news:1106177977.669031.53940@z14g2000cwz.googlegro ups.com...
>> > Mike Wahler wrote:
>> >> "Neo" <timeless_illusion@yahoo.com> wrote...
>> >> > Why the following code is compilable? The function abc() doesn't
>> >> > take any arguments, still i can call the function with arbitraty
>> >> > number of arguments.
>> >> > Even compiler doesn't show any warning. What the standard says?
>> >> >
>> >> > ----- file1.c ------
>> >> > extern unsigned abc();
>> >>
>> >> This prototype does not indicate 'no arguments'.
>> >
>> > It's not a prototype (in C.) It's only a function declaration.

>>
>> What is the difference between a function declaration and a function
>> prototype?
>>
>> >
>> >> It indicates 'arguments not specified'. It's
>> >> also a fragile way to do things.
>> >
>> > --
>> > Peter
>> >

>>
>> -Neo

>
> I already posted an extensive answer to your original post in
> comp.arch.embedded.
>
> In the future, if you are going to post the same article to multiple
> groups, after making sure that it is topical in the multiple groups,
> cross-post it, that is put all the newsgroup names in a single post.


O'kay, Thanks JK.

>
> As to this question, a function prototype specifies the name of a
> function, the type it returns, and the number and types of arguments:
>
> int fred(double ricky, char *ethel);
>
> ...is a prototype, as is:
>
> int fred(double, char *);
>
> ...because names for the arguments are optional except when you define
> the body of a function.
>
> Every line that specifies a function name, return type, and a pair of
> parentheses is a function declaration. If it also includes the types
> of the parameters, it is a prototype.
>
> --
> 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





All times are GMT. The time now is 06:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.