Article View: comp.std.c
Article #163Re: #if and #endif in different files: legal?
From: Jos Horsmeier
Date: Thu, 30 Jul 1992 08:53
Date: Thu, 30 Jul 1992 08:53
59 lines
1782 bytes
1782 bytes
In article <ntvfii4@fido.asd.sgi.com> shankar@sgi.com (Shankar Unni) writes:
|
|What is the accepted opinion as to the legality of a "#if" and a
|"#else" or "#endif" being in different files, as in:
|
| a.c:
|
| #ifdef __STDC__
| #include "a.h"
|
| a.h:
|
| main() { }
| #endif /* __STDC__ */
|
|
|Should the above compile? Not compile? Implementation-defined?
It shouldn't compile. Have a look at the syntax of a preprocessing-file:
ANSI 3.8 Preprocessing directives:
preprocessing-file:
group_opt
group:
group-part
...
group-part:
if-section
control-line
...
if-section:
if-group elif-groups_opt else-group_opt endif-line
control-line:
#include pp-tokens new-line
...
As you can see, the entire #if ... #endif structure has to be present
in _one_ preprocessing file. The #include control line has to be processed
recursively as stated in ANSI 2.1.1.2 Translation phases:
`A #include preprocessing directive causes the named header or source file
to be processed from phase 1 through phase 4 recursively.'
A footnote states:
`Implementations must behave as if these separate phaases occur, even
though many are typically folded together in practice.'
So an #include directive is a bit more than just a switch from one
file to another for the lexical analyzer (after stacking the previous
file descriptors of course.)
kind regards,
Jos aka jos@and.nl
Technical Details
Message-ID:
<3181@dozo.and.nl>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!sun4nl!and!josReferences:
<ntvfii4@fido.asd.sgi.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sun4nl!and!jos From: jos@and.nl (Jos Horsmeier) Newsgroups: comp.std.c Subject: Re: #if and #endif in different files: legal? Keywords: preprocessing Message-ID: <3181@dozo.and.nl> Date: 30 Jul 92 08:53:02 GMT References: <ntvfii4@fido.asd.sgi.com> Organization: AND Software BV Rotterdam Lines: 59