Search code examples
cfunction-call

Calling of C functions with undefined number of parameters


Note this question does not refer to ellipsis.

Consider the following code

#include <stdio.h>

void foo() {
    printf("I AM AWESOME\n");
}

main(void) {
    foo(1,2,3);
    foo();
return 0;
}

This program runs perfectly and provides the output. However, in case of 'main', this works irrespective of

main(void)

or

main()

When, defining foo as

foo(void)

gives an error - "too many arguments".

If both are functions, shouldn't they also follow the same rules?


Solution

  • When you declare a function without parameters it means to disable type checking and to use K&R calling convention. It does not mean that the function does not have parameters.

    In ANSI when you want to explicitly say that the function does not have parameters, you need to declare it as fun(void).