Two function declarations with void and empty argument list

青春壹個敷衍的年華 提交于 2019-12-05 06:58:29

Quoting a late draft of the standard about function declarators:

(6.7.6.3/10) The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

(6.7.6.3/14) An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.

So the declarators of the declaration and definition are in fact compatible, and thus refer to the same function (with no overloading taking place of course, as such a thing does not exist in C.)

The line below is a function declaration, which tells the signature of the function foo: what is the type of the returned value and what are the types of the arguments.

void foo(void);

Below there is a function definition, which tells what does the function do. It does not overload anything. The definition and the declaration must match in the signature. void data type allows omitting it in the function definition.

void foo()
{
}

Since void is not an instantiable type (you cannot have a value of type void) it is OK to omit the arguments in the signature of the function's definition. However, if you try to do:

void foo(void*);
void foo() {
}

then you'll have a compile error because foo is expected to get a pointer to a don't-worry-about-type value.

perilbrain

C Standard defines for void as:-

The void type comprises an empty set of values; it is an incomplete type that cannot be completed.

And the definition

void foo()
{
}

implies that the parameters are empty set of values which is valid for definition,so the gcc allows.

Also prototype for function declaration specifies:-

The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!