用存储类说明符声明一个变量但没有类型说明符是什么意思? [英] What does it mean to declare a variable with a storage class specifier but no type specifier?
问题描述
阅读完 ANSI C Yacc语法后规范我注意到以下都是有效的:
register x;
auto y;
static z;
extern q;
这对我来说似乎很奇怪,因为我对类型的理解表明这些变量都没有类型。这些是什么意思?他们如何检查类型?在C99之前,如果没有指定类型,默认为 int / code>这应该在C99中被删除,但许多编译器甚至在C99模式下都支持它。例如,在
clang
甚至使用 -std = c99
时,我只收到以下警告而不是错误:
警告:类型说明符缺失,默认为'int'[-Wimplicit-int]
寄存器x;
~~~~~~~~ ^
warning:类型说明符缺失,默认为'int'[-Wimplicit-int]
auto y;
~~~~ ^
warning:类型说明符缺失,默认为'int'[-Wimplicit-int]
static z;
~~~~~~ ^
warning:类型说明符缺失,默认为'int'[-Wimplicit-int]
extern q;
~~~~~~ ^
如果我们看一下草案C99标准 转发部分说: [.. 。]以前版本的主要变化包括: 包括以下项目符号: - 删除隐式int 更新 从国际标准编程语言的基本原理-C部分 C99的新特性:在C89中,所有类型说明符可以从$ b声明中的$ b说明符。在这种情况下int是暗含的。委员会认为这一特征的b $ b固有危险超过了其便利性,因此被删除。这样做的效果是保证生成一个能够捕获另一类编程错误的诊断。发布诊断之后,实现可以选择假设隐式int并继续翻译该程序,以支持利用此功能的现有源代码。 您使用的语法早于C99 ,但据我所知,新版本,该版本已更新以反映 C11 与声明中的类型说明符相差不大。所以这种情况下的语法不足以执行这个约束。您必须转到标准部分 至少应在每个声明的声明说明符中以及每个struct声明和类型名称的说明符限定符列表中给出一个类型说明符。 After reading through the ANSI C Yacc grammar specification I noticed the following are all valid: This seems strange to me, as my understanding of type would suggest that none of these variables have a type. What do these mean? How are they type checked? How much memory is allocated? Before C99 if a type was not specified it defaulted to If we look at the draft C99 standard the Forward section says: [...]Major changes from the previous edition include: and includes the following bullet: — remove implicit int Update From the Rationale for International Standard—Programming Languages—C section new feature of C99: In C89, all type specifiers could be omitted from the declaration
specifiers in a declaration. In such a case int was implied. The Committee decided that the
inherent danger of this feature outweighed its convenience, and so it was removed. The effect is to guarantee the production of a diagnostic that will catch an additional category of programming errors. After issuing the diagnostic, an implementation may choose to assume an implicit int and continue to translate the program in order to support existing source code that exploits this feature. The grammar you are using does predate C99 but as far as I can tell a newer version which is updated to reflect C11 does not differ much with respect to type specifiers in a declaration. So the grammar in this situation is not sufficient to enforce this constraint. You would have to goto the standard section 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.
这篇关于用存储类说明符声明一个变量但没有类型说明符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! gcc
也只在这种情况下提供警告,尽管使用 -pedantic-errors
标志将导致 gcc
在 gcc
中通常会遇到错误,通常是
6.7.2
6.7.2
类型说明符并看到它说:
register x;
auto y;
static z;
extern q;
int
this was supposed to be removed in C99 but many compilers support it even in C99 mode. For example in clang
even using -std=c99
I only receive the following warnings as opposed to errors:warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
register x;
~~~~~~~~ ^
warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
auto y;
~~~~ ^
warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
static z;
~~~~~~ ^
warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
extern q;
~~~~~~ ^
gcc
also only provides a warning in this case, although using the -pedantic-errors
flag will cause gcc
to produce errors which is usually the case for extensions in gcc
and usually for clang
to but not in this case.
6.7.2
Type specifiers:
6.7.2
Type specifiers and see that it says: