Number of arguments mismatch in function call
Andrew Haley
aph@redhat.com
Thu May 20 16:35:00 GMT 2010
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