Auto valid translation unit C?

Create a test.c file containing the following code:

 auto; 

Compile it with clang 6.0: clang -c test.c It will successfully generate the test.o object file, although without the actual contents (except for the headers of the object files). It prints a warning, but nonetheless accepts this as a valid code:

 test.c:1:1: warning: declaration does not declare anything [-Wmissing-declarations] auto; ^~~~ 1 warning generated. 

Unlike gcc 4.9, it refuses to compile test.c , generating an error:

 test.c:1:1: error: 'auto' in file-scope empty declaration auto; ^ 

Why does clang generate a warning, but this translation unit is considered valid, and gcc generates an error and refuses to compile it? Whose behavior is more in line with C standards? What is the point of an automatic ad that declares nothing?

+6
source share
1 answer

Invalid code. But the implementation is free to give it whatever value it wants after issuing a diagnostic message.

5.1.1.3 Diagnostics

1 The corresponding implementation must contain at least one diagnostic message (indicated in the one defined by the implementation) if the translation block or the translation block for preliminary processing contains a violation of any syntax rule or restriction, even if the behavior is also explicitly specified as undefined or defined for the implementation. Diagnostic messages need not be produced in other circumstances. 9)
[...]

6.7 Announcements

Limitations
2. In a declaration other than a static_assert declaration, at least a declarator (except for function parameters or members of a structure or union), a tag, or enumeration members must be declared.
[...]

Quotes from C99 + Amendments (C11, n1570)

+4
source

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


All Articles