Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

language design - Why is this legal in C?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...