Implicit int in c language

I am using the orwell dev C ++ IDE. I know that in the old C89 standard and C89 standard C ++ , the default rule for int is supported when the type of the return function is not explicitly specified in the function definition. But it is forbidden in C ++. But I recently wrote the following simple C program, and it works great.

 #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 the default rule applies to variables? My compiler shows me the 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 does the C99 still allow int to be used by default? This fails in compiling in C ++. Correct me if I am wrong? This C program also works with compilers in a line, for example ideone.com

+2
source share
2 answers

This is explained in the C99 rationale :

New C99 Feature:

In C89, all type specifiers can be omitted from the declaration specifiers in the declaration. In this case, int was implied. The committee decided that the inherent danger of this feature outweighed its convenience, and therefore it was removed. The effect is to guarantee the production of diagnostic data that will understand the additional category of programming error. After issuing the diagnostics, the implementation can select the intended int and continue translating the program to support existing source code that uses this function.

In other words, it is officially removed from the C99 standard, but compilers can still choose to follow this behavior and issue diagnostics like GCC does. For example, view the alert options page for -Wimplicit-int . To have these warnings compiled as errors, use -pedantic-errors or -Werror .

According to @ Anonymous Answer, C ++ 98 contains a similar rule about type specifiers.

7.1.5 / 2

At least one qualifier type is required that is not a cv qualifier in the declaration, unless it declares a constructor, destructor, or conversion function. 80)

80) There is no special provision for decl-specifier-seq that does not have a type specifier or has a type specifier that defines only cv qualifiers. The "implicit int" rule for C is no longer supported.

For example, GCC supports ISO / IEC 14882: 1998 and higher, so it will be a bug, no matter what.

+8
source

The C99 standard does not allow the omission of types.

Section 6.7.2.2 states:

The statement must contain at least one qualifier type qualifier in each declaration and in the list of qualifier-qualifiers in each structure declaration and type name.

+2
source

Source: https://habr.com/ru/post/1205363/


All Articles