home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / std / c / 2387 < prev    next >
Encoding:
Text File  |  1992-07-29  |  1.7 KB  |  71 lines

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.std.c
  4. Subject: Re: #if and #endif in different files: legal?
  5. Keywords: preprocessing
  6. Message-ID: <3181@dozo.and.nl>
  7. Date: 30 Jul 92 08:53:02 GMT
  8. References: <ntvfii4@fido.asd.sgi.com>
  9. Organization: AND Software BV Rotterdam
  10. Lines: 59
  11.  
  12. In article <ntvfii4@fido.asd.sgi.com> shankar@sgi.com (Shankar Unni) writes:
  13. |
  14. |What is the accepted opinion as to the legality of a "#if" and a
  15. |"#else" or "#endif" being in different files, as in:
  16. |
  17. | a.c:
  18. |
  19. |    #ifdef __STDC__
  20. |    #include "a.h"
  21. |
  22. | a.h:
  23. |
  24. |    main() { }
  25. |    #endif /* __STDC__ */
  26. |
  27. |
  28. |Should the above compile? Not compile? Implementation-defined?
  29.  
  30. It shouldn't compile. Have a look at the syntax of a preprocessing-file:
  31. ANSI 3.8 Preprocessing directives:
  32.  
  33. preprocessing-file:
  34.     group_opt
  35.  
  36. group:
  37.     group-part
  38.     ...
  39.  
  40. group-part:
  41.     if-section
  42.     control-line
  43.     ...
  44.  
  45. if-section:
  46.     if-group elif-groups_opt else-group_opt endif-line
  47.  
  48. control-line:
  49.     #include pp-tokens new-line
  50.     ...
  51.  
  52. As you can see, the entire #if ... #endif structure has to be present
  53. in _one_ preprocessing file. The #include control line has to be processed
  54. recursively as stated in ANSI 2.1.1.2 Translation phases:
  55.  
  56. `A #include preprocessing directive causes the named header or source file
  57.  to be processed from phase 1 through phase 4 recursively.'
  58.  
  59. A footnote states:
  60.  
  61. `Implementations must behave as if these separate phaases occur, even
  62.  though many are typically folded together in practice.'
  63.  
  64. So an #include directive is a bit more than just a switch from one
  65. file to another for the lexical analyzer (after stacking the previous
  66. file descriptors of course.)
  67.  
  68. kind regards,
  69.  
  70. Jos aka jos@and.nl
  71.