Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is auto; a valid C translation unit?

Tags:

c

gcc

clang

Create a file test.c, containing the following code:

auto;

Compile it with clang 6.0: clang -c test.c. It will successfully generate an object file test.o, albeit one with no actual content (other than the object file headers). It prints a warning, but nonetheless accepts this as valid code:

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

By contrast, gcc 4.9 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 yet accept this translation unit as valid, while gcc generates an error and refuses to compile it? Whose behaviour is more conforming to the C standards? What is the point of allowing an auto declaration which declares nothing?

like image 595
Simon Kissane Avatar asked Nov 09 '14 01:11

Simon Kissane


Video Answer


1 Answers

It is not valid code. But the implementation is free to give it any meaning it wants after issuing a diagnostic message.

5.1.1.3 Diagnostics

1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
[...]

6.7 Declarations

Constraints
2 A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.
[...]

Quotes from C99+Amendments (C11, n1570)

like image 77
Deduplicator Avatar answered Oct 27 '22 13:10

Deduplicator