简体   繁体   中英

implicit int in c language

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

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]

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

This is explained in the C99 rationale :

A 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.

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 .

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

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

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

The C99 standard does not allow types to be omitted.

Section 6.7.2.2 says:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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