Why is this legal in C?

c, language-design

Solution

A function with no listed arguments in the prototype is deemed to have an indeterminate number, not zero.

If you really want zero arguments, it should be:

void foo (void);

The empty-list variant is a holdover from ancient C, even before ANSI got their hands on it, where you had things like:

add_one(val)
int val;
{
    return val + 1;
}

(with `int` being the default return type and parameter types specified outside the declarator).

If you're doing a toy compiler and you're not worried about compliance with every tiny little piece of C99, I'd just toss that option out and require a parameter list of some sort.

It'll make your life substantially easier, and I question the need for people to use that "feature" anyway.

Problem

I am writing a toy C compiler for a compiler/language course at my university. I'm trying to flesh out the semantics for symbol resolution in C, and came up with this test case which I tried against regular compilers clang & gcc. ``` void foo() { } int main() { foo(5); } // foo has extraneous arguments ``` Most compilers only seem to warn about extraneous arguments. Question: What is the fundamental reasoning behind this? For my symbol table generation/resolution phase, I was considering a function to be a symbol with a return type, and several parametrized arguments (based on the grammar) each with a respective type. Thanks.

Original source