3

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

klutt
  • 30,332
  • 17
  • 55
  • 95
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 4
    *Why C99 standard still allows default to int?* My guess is that if it didn't, a lot of old code would break. – R Sahu Oct 04 '14 at 05:35
  • Why apply the rules from the past? Slavery, Burning witches.... Anyway C++ and C are two different languages. Just happen to have C in their name and have a common heritage – Ed Heal Oct 04 '14 at 05:43
  • 1
    "Is it true that default int rule is also applied to variables?": yes, C90's list of permitted type specifiers (in 6.5.2) contained the following entry as one of the permitted options (emphasis added): "int, signed, signed int, **or no type specifiers**". C99 removed that last bit. – Michael Burr Oct 04 '14 at 05:54
  • 1
    Related to: [Which section in C89 standard allows the “implicit int” rule?](http://stackoverflow.com/q/26488502/1708801). – Shafik Yaghmour Oct 23 '14 at 17:21

2 Answers2

9

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.

2

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.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118