在C语言中隐含INT [英] implicit int in c language

查看:277
本文介绍了在C语言中隐含INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用奥威尔开发的C ++ IDE。我知道,在旧的 C89 标准和放大器; pre标准C ++支持的默认为int 当函数返回类型在函数定义中明确指定的规则。但它已禁止在C ++中。但最近我写了下面这个简单的C程序和放大器;它工作正常。

I am using 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. But it has banned in C++. But recently i wrote following simple C program & it works fine.

#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);
}

这是真的,默认INT规则也适用于变量?我的编译器显示我下面的警告。

Is it true that default int rule is also applied to variables? 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]

为什么 C99 标准仍允许默认为int?它没有在C ++编译。纠正我,如果我错了?这个C程序也适用于在线编译器像ideone.com

Why C99 standard still allows default to int? It fails in compilation in C++. Correct me if i am wrong? This C program also works on on line compilers like ideone.com

推荐答案

这是在的 C99理由

C99的一个新特点:

A new feature of C99:

在C89中,所有类型说明符可以从声明中省略
  符在声明。在这种情况下INT被暗示。该
  委员会决定,该功能的潜在危险抵销
  它的方便,因此它已被删除。效果是保证
  生产诊断的,将捕​​获的其他类别
  编程错误。发出诊断后,实施
  可以选择承担隐含的int和继续以支持现有的源$ C ​​$ C,它利用此功能的程序进行转换。

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.

在换句话说,它正式从C99标准中删除,但是编译器仍然可以选择遵循这一行为,并发出诊断,因为GCC一样。例如,查看他们的警告选项 -Wimplicit-INT 。为了使这些警告的编译错误,使用 -pedantic-错误 -Werror

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. For example, view their warning options page for -Wimplicit-int. To make these warnings compile as errors, use -pedantic-errors or -Werror.

根据@匿名的回答,请 C ++ 98 包含有关类型说明符类似的规则。

As per @Anonymous's answer, c++98 contains a similar rule about type specifiers.

7.1.5 / 2

7.1.5/2

至少有一个的不是一个的 CV-预选赛的需要的类型说明符
  在声明中,除非它声明了一个构造函数,析构函数或
  转换功能。 80)

80)有没有特殊规定的
  的 DECL说明符-SEQ 的一个缺乏的类型说明符的或具有的类型说明符的,只有指定的 CV-预选赛。 C的隐式int规则不再支持。

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. The "implicit int" rule of C is no longer supported.

例如, GCC 支持ISO / IEC 14882:1998以上,所以这将是一个错误,不管是什么。

For example, GCC supports ISO/IEC 14882:1998 and above, so this would be an error no matter what.

这篇关于在C语言中隐含INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆