- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尽管许多编译器都可以容忍没有返回类型的函数,但 N1570 的标准似乎没有规范性的措辞。为什么会这样?
最佳答案
在最初的 C 标准(C89、C90)中,第 6.7.1 节涵盖了函数定义。在 C99 和 C11 标准中,第 6.9.1 节涵盖了函数定义。两者之间有一个很小但至关重要的区别。
ISO/IEC 9899:1990
§6.7.1 Function definitions
Syntax
function definition:
declaration-specifiersopt declarator declaration-listopt compound-statement
§6.9.1 Function definitions
Syntax
¶1 function definition:
declaration-specifiers declarator declaration-listopt compound-statement
¶2 The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified in the declarator portion of the function definition.
¶3 The return type of a function shall be
void
or a complete object type other than array type.
static
或
extern
而没有类型)。在 C90 中,这是合法的;在 C99 和 C11 中,这是不允许的。与此相关的部分是 C90 中的 §6.5.2 和 C99 和 C11 中的 §6.7.2,用于类型说明符。
Type specifiers
Constraints
Each list of type specifiers shall be one of the following sets (delimited by commas, when there is more than one set on a line); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.
6.7.2 Type specifiers
Constraints
At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following sets (delimited by commas, when there is more than one set on a line); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.
old-style.c
中的草率(按照现代标准)代码:
function(int x)
{
return 2 * x * x + 3 * x - 7;
}
$ gcc -c old-style.c
old-style.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
function(int x)
^~~~~~~~
$ gcc -std=c11 -c old-style.c
old-style.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
function(int x)
^~~~~~~~
$ gcc -std=c99 -c old-style.c
old-style.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
function(int x)
^~~~~~~~
$ gcc -std=c90 -c old-style.c
$
-Werror
或
-Werror=implict-int
将这些警告转换为错误:
$ gcc -Werror -std=c11 -c old-style.c
old-style.c:1:1: error: return type defaults to ‘int’ [-Werror=implicit-int]
function(int x)
^~~~~~~~
cc1: all warnings being treated as errors
$ gcc -Werror=implicit-int -std=c11 -c old-style.c
old-style.c:1:1: error: return type defaults to ‘int’ [-Werror=implicit-int]
function(int x)
^~~~~~~~
cc1: some warnings being treated as errors
$
-Werror
将所有警告变为错误,但
-Werror=implicit-int
将该警告变为错误(其他警告仍为警告)。
$ gcc -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes \
> -Wold-style-declaration -Wold-style-definition -std=c11 -c old-style.c
old-style.c:1:1: error: return type defaults to ‘int’ [-Werror=implicit-int]
function(int x)
^~~~~~~~
old-style.c:1:1: error: no previous prototype for ‘function’ [-Werror=missing-prototypes]
cc1: all warnings being treated as errors
$
-Wold-style-xxx
选项,而 Clang 不喜欢
-Wold-style-declaration
选项,所以我经常省略。
new-style.c
相当容易:
extern int function(int x); /* Should be in a header */
int function(int x)
{
return 2 * x * x + 3 * x - 7;
}
$ gcc -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes \
> -Wold-style-declaration -Wold-style-definition -std=c11 -c new-style.c
$
关于c - 该标准是否对没有返回类型的函数有任何规范性措辞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34515884/
我是一名优秀的程序员,十分优秀!