简体   繁体   English

c语言中的隐式int

[英]implicit int in c language

I am using orwell dev c++ IDE.我正在使用 orwell dev c++ IDE。 I know that in the old C89 Standard & pre standard C++ supports default to int rule when return type of function isn't explicitly specified in function definition.我知道在旧的C89标准和预标准中,当函数定义中没有明确指定函数的返回类型时,C++ 支持默认为 int规则。 But it has banned in C++.但它在 C++ 中已被禁止。 But recently i wrote following simple C program and it works fine.但最近我写了以下简单的 C 程序,它工作正常。

#include <stdio.h>
void fun();
int main(void)
{
    int a=9;
    printf("%d",a);
    printf("%d",a);
    fun();
    return 0;
}
a=1;
void fun()
{
    printf("%d",a);
}

Is it true that default int rule is also applied to variables?默认 int 规则是否也适用于变量? My compiler shows me following warnings.我的编译器向我显示以下警告。

[Warning] data definition has no type or storage class [enabled by default] 

[Warning] type defaults to 'int' in declaration of 'a' [enabled by default]

Why C99 standard still allows default to int?为什么C99标准仍然允许默认为 int? It fails in compilation in C++.它在 C++ 中编译失败。 Correct me if i am wrong?如果我错了,请纠正我? This C program also works on on line compilers like ideone.com这个 C 程序也适用于像 ideone.com 这样的在线编译器

This is explained in the C99 rationale :这在C99 原理中有解释:

A new feature of C99: C99的一个新特性:

In C89, all type specifiers could be omitted from the declaration specifiers in a declaration.在 C89 中,可以从声明中的声明说明符中省略所有类型说明符。 In such a case int was implied.在这种情况下,int 是隐含的。 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.发出诊断后,实现可以选择假定隐式 int 并继续翻译程序,以支持利用此功能的现有源代码。

In other words, it's officially removed from the C99 standard, but compilers may still choose to follow this behavior and issue a diagnostic, as GCC does.换句话说,它已正式从 C99 标准中删除,但编译器可能仍会选择遵循此行为并发出诊断信息,就像 GCC 所做的那样。 For example, view their warning options page for -Wimplicit-int .例如,查看-Wimplicit-int警告选项页面。 To make these warnings compile as errors, use -pedantic-errors or -Werror .要将这些警告编译为错误,请使用-pedantic-errors-Werror

As per @Anonymous's answer, c++98 contains a similar rule about type specifiers.根据@Anonymous 的回答, c++98包含有关类型说明符的类似规则。

7.1.5/2 7.1.5/2

At least one type-specifier that is not a cv-qualifier is required in a declaration unless it declares a constructor, destructor or conversion function.声明中至少需要一个不是cv 限定符的类型说明符,除非它声明了构造函数、析构函数或转换函数。 80) 80)

80) There is no special provision for a decl-specifier-seq that lacks a type-specifier or that has a type-specifier that only specifies cv-qualifiers . 80)对于缺少类型说明符或具有仅指定cv-qualifiers类型说明符decl-specifier-seq没有特殊规定。 The "implicit int" rule of C is no longer supported.不再支持 C 的“隐式 int”规则。

For example, GCC supports ISO/IEC 14882:1998 and above, so this would be an error no matter what.例如, GCC支持 ISO/IEC 14882:1998 及更高版本,因此无论如何这都会是一个错误。

The C99 standard does not allow types to be omitted. C99 标准不允许省略类型。

Section 6.7.2.2 says:第 6.7.2.2 节说:

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.每个声明的声明说明符中以及每个结构声明和类型名称的说明符-限定符列表中应至少给出一个类型说明符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM