Re: Number of arguments mismatch in function call

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 05/21/2010 04:38 AM, Sulabh Nangalia wrote:
> Thanks Andrew.
> Do you mean that ANSI C declares prototype
> of a function by default (as in case 3 here where
> I have not declared any prototype explicitly) ?

No, but in that case you have used the ANSI form of a function definition
rather than the K&R.

> On Thu, May 20, 2010 at 7:52 PM, Andrew Haley <aph@xxxxxxxxxx> wrote:
>> On 05/20/2010 02:24 PM, Sulabh Nangalia wrote:
>>>
>>> I have a question with function definition.
>>> Please consider following 3 different forms
>>> of writing a same test example:
>>>
>>> 1.
>>> static int foo(i, j)
>>>   int i;
>>>   float j;
>>> {
>>>   return i;
>>> }
>>> int main()
>>> {
>>>   return foo(1);    // Passing less arguments
>>> }
>>>
>>> 2.
>>> static int foo(int, float);
>>> static int foo(i, j)
>>>   int i;
>>>   float j;
>>> {
>>>   return i;
>>> }
>>> int main()
>>> {
>>>   return foo(1);    // Passing less arguments
>>> }
>>> test.c: In function 'main':
>>> test.c:10: error: too few arguments to function 'foo'
>>>
>>> 3.
>>> static int foo(int i, float j)
>>> {
>>>   return i;
>>> }
>>> int main()
>>> {
>>>   return foo(1);    // Passing less arguments
>>> }
>>>
>>>
>>> I am using gcc version 4.1.2 to compile all the 3 variations as:
>>> % gcc test.c
>>>
>>>
>>> The first one compiles fine.
>>> While the 2nd & 3rd give following error:
>>> test.c: In function 'main':
>>> test.c:7: error: too few arguments to function 'foo'
>>>
>>> Can someone please explain if this is a bug of gcc
>>> or a desired behavior and why?
>>
>> It's desired behaviour.  The first version of your function has no
>> prototype, the others do, so the error is caught with 2 and 3.  This
>> is one of the differences between ANSI C and K&R C: ANSI C has
>> prototypes.  Do not use the first form: it's only supported for
>> compatibility with ancient programs.
>>
>> Andrew.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux