C The compiler does not throw an error if the type is not specified

Why this program below does not throw an error:

dfljshfksdhfl; #include <stdio.h> int main () { return 0; } 

gcc will simply give a warning:

test.c: 1: 1: warning: data definition does not have a type or storage class [enabled by default]

+5
source share
3 answers

This is because, although the implicit int is no longer part of the C standard with C99 , some compilers still support it, mainly to prevent a lot of old code from breaking. So this line:

 dfljshfksdhfl; 

ends with the equivalent:

 int dfljshfksdhfl; 

clang gives us a more informative warning by default:

 warning: type specifier missing, defaults to 'int' [-Wimplicit-int] dfljshfksdhfl; ^~~~~~~~~~~~~ 

We can use the - pedantic-errors flag to turn this into an error, although, oddly enough, this does not work for clang and therefore we must resort to -Werror and turn all warnings into errors, which is actually a good habit. As the remark points to clang , we can also use -Werror=implicit-int .

+7
source

I already answered a similar question (in fact, I'm sure this is a duplicate, but whatever), and the answer is found in the rationale for C99.

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 may prefer an implicit int and continue translating the program to support existing source code that uses this feature.

The answers

@Shafik tells you about one way to turn a warning into an error (for Clang). If you think -Werror too strict, you can include this warning in a message with -Werror=implicit-int . In GCC, it -pedantic-errors that -pedantic-errors necessary.

+1
source

First of all, gcc is not a compatible C compiler by default. It implements the C89 / C90 dialect with GNU extensions.

You can use -std=cNN -pedantic (where NN can be 90 , 99 or 11 ) to force (try) to match the specified version of the ISO standard. C90 implicit int allowed; It was reset on the C99.

But C compilers do not really need to generate fatal error messages (with the exception of the #error directive). Standard requirement ( N1570 5.1.1.3p1):

The corresponding implementation must give at least one diagnostic message (defined according to the implementation) if the translation unit for preprocessing or the translation unit violates any syntax rule or restriction, even if the behavior is also explicitly specified as undefined or determined by the implementation. Diagnostic messages should not occur under other circumstances.

Non-fatal warning qualifies as a "diagnostic message." The corresponding C compiler can print a warning for any error - even a syntax error - and then continue to successfully compile the source file. (Here's how some language extensions for the compiler can be supported.)

Personally, I find gcc too weak due to certain errors; in my opinion, the lack of int should be seen as a fatal error. But this is only my preference, and not the requirement imposed by the standard.

The lesson here is that you should not allow simple warnings to be harmless. Ideally, code compilation should not produce any diagnostics. Cases where it is normal to ignore warnings are rare (but they exist because compilers can warn about perfectly correct code).

0
source

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


All Articles