Number of arguments mismatch in function call
Sulabh Nangalia
sulabh.nangalia@gmail.com
Fri May 21 06:29:00 GMT 2010
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) ?
Regards,
Sulabh
On Thu, May 20, 2010 at 7:52 PM, Andrew Haley <aph@redhat.com> 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.
More information about the Gcc-help
mailing list