ANSI C 函数声明中隐式 int return 类型的规则
Rules for implicit int return type in ANSI C function declarations
我知道 f(void) { return 0; }
有一个 return 类型的 int
,即使它没有指定,但是下面的呢:
(*f())[]{}
(*g())(){}
const*h(){}
使用 -ansi -Werror -pedantic
在 gcc 上编译时没有错误,仅抱怨 clang 中缺少 return 语句:https://godbolt.org/z/jAYL4v
f
似乎有 return 类型的 int(*)[]
,g
似乎有 return 类型的 int(*)()
,并且h
似乎有一个 return 类型的 const int*
。
在 ANSI C 标准中,我似乎找不到这方面的规则。我检查了 [6.5.4.3 函数声明符(包括原型)] 和 [6.7.1 函数定义],但甚至找不到关于隐式 int return 类型的任何内容(某些示例代码除外)。我只是找错地方了吗?这是有效代码吗?
函数定义 (C89 3.7.1) 的语法是:
function-definition:
declaration-specifiersopt declarator declaration-listopt compound-statement
与C99相同,只是declaration-specifiers在C89中是可选的。
这些语法术语的含义是:
- declaration-specifiers 是 storage-class-specifier、type-specifier 的任意组合,以及type-qualifier;其中的示例分别为
static
、float
、const
。
- declarator,非正式地,可以出现在这里的任何东西:
int ______ ;
。它当然有一个正式的定义,但我提到了这种方法,因此您可以将术语与您对声明语法的现有理解联系起来。
- declaration-list指K&R-style参数声明。
- compound-statement 是语句的括号列表(可能为空)。
在您的问题中,以下是声明符:(*f())[]
(*g())()
*h()
。所以在它们后面跟着 {}
(即 compound-statement)是合法的。 h
还有一个declaration-specifier。
在C89中还有一条规则(3.5.2)在一组declaration-specifiers[=中不提供type-specifier 48=]相当于提供int
。 C99 添加了约束 "At least one type specifier shall be given in the declaration specifiers in each declaration [...]".
我知道 f(void) { return 0; }
有一个 return 类型的 int
,即使它没有指定,但是下面的呢:
(*f())[]{}
(*g())(){}
const*h(){}
使用 -ansi -Werror -pedantic
在 gcc 上编译时没有错误,仅抱怨 clang 中缺少 return 语句:https://godbolt.org/z/jAYL4v
f
似乎有 return 类型的 int(*)[]
,g
似乎有 return 类型的 int(*)()
,并且h
似乎有一个 return 类型的 const int*
。
在 ANSI C 标准中,我似乎找不到这方面的规则。我检查了 [6.5.4.3 函数声明符(包括原型)] 和 [6.7.1 函数定义],但甚至找不到关于隐式 int return 类型的任何内容(某些示例代码除外)。我只是找错地方了吗?这是有效代码吗?
函数定义 (C89 3.7.1) 的语法是:
function-definition: declaration-specifiersopt declarator declaration-listopt compound-statement
与C99相同,只是declaration-specifiers在C89中是可选的。
这些语法术语的含义是:
- declaration-specifiers 是 storage-class-specifier、type-specifier 的任意组合,以及type-qualifier;其中的示例分别为
static
、float
、const
。 - declarator,非正式地,可以出现在这里的任何东西:
int ______ ;
。它当然有一个正式的定义,但我提到了这种方法,因此您可以将术语与您对声明语法的现有理解联系起来。 - declaration-list指K&R-style参数声明。
- compound-statement 是语句的括号列表(可能为空)。
在您的问题中,以下是声明符:(*f())[]
(*g())()
*h()
。所以在它们后面跟着 {}
(即 compound-statement)是合法的。 h
还有一个declaration-specifier。
在C89中还有一条规则(3.5.2)在一组declaration-specifiers[=中不提供type-specifier 48=]相当于提供int
。 C99 添加了约束 "At least one type specifier shall be given in the declaration specifiers in each declaration [...]".