4 ms·
Small nitpick. Functions in C that take no arguments are written name(void){...} not name(){...}. This latter form is an old-style definition which doesn't intr
by kazinator 8y ago
Small nitpick. Functions in C that take no arguments are written name(void){...} not name(){...}. This latter form is an old-style definition which doesn't introduce a prototype into the scope.
For functions that have prototypes, it is okay, but when they do not, you're losing type checking (yes, even on the number of arguments).
The following program compiles with no diagnostics for me with -W -Wall -ansi -pedantic, with GCC 7.3 on Ubuntu 18:
int func()
{
return 0;
}
int main()
{
func(3);
return 0;
}
-std=c11 (as you're using) makes no difference.
The same is not true of C++: func() in C++ is a prototype definition. C++ supports (void) for compatibility with C, but even in nonsensical contexts: class::class(void);
- sesutton 8y agoClang (tested with v7) however does issue a warning even with no flags set.
- kazinator 8y agoAny compiler should because as a definition, int foo() { } does in fact inform us that the function can only be correctly called with no arguments. Formally, this is not introduced into the scope as type information according to the language spec, but that's no reason not to warn about it informally.
- rui314 8y agoQuote from 6.7.6.3.14 of the C++11 spec (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf): > 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. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. So, my interpretation is that the following two function definitions define the same function of the exact same type void func() {} void func(void) {} although the following two function declarations declare two functions of different types void func(); void func(void);
- deleted 8y ago[deleted]
- kazinator 8y agoThat is of course the C11 (draft) spec, not C++. Interesting find there. The wording is also in the C99 draft; it is not new. It is in fact saying that the empty list in a definition is a special case and does declare that the function takes no parameters. To "specify" here can be understood to mean as inserting information about type into the declaration scope; that which a declaration does. So that's a bit of a bug in GCC there; it should be treating this the same as (void) and therefore diagnosing that way. If not by default, then at least when -pedantic is applied. But nope: $ gcc -Wall -W -std=c11 -pedantic proto.c $
- nezirus 8y agoMy understanding is that, unless you have a function prototype, you will lose type info about the parameters: "The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied." i.e. the most important is the end of that sentence, so GCC behavior should be right.
- kazinator 8y agoNote the: "that is not part of a definition". This is about declarations only.