Reputation: 13196
I'm trying to write a state machine that slurps a source file and splits it into sections that are either the compiler's business or the preprocessor's business. Not a deep traversal, I'm just looking for sections that are either comments or preprocessor directives. (no macros, no conditionally compiled blocks, etc.)
Comments are simple enough, but I'm not 100% sure where it's legal to specify a preprocessor directive. For example, is the following line legal?
int i; #include <derp.h>
Are there any special cases where some directives are allowed and others are not?
I've searched google and SO and not found a question which answers this.
Please answer for BOTH C and C++, I tagged both knowingly and intentionally.
Upvotes: 6
Views: 235
Reputation: 372664
Preprocessor directives can appear anywhere, as long as they're the first non-whitespace token on the line. Accordingly, you can't write
int i; #define ThisIsntLegal SinceItsNotAtTheStart
But this would be:
int i;
#define Woohoo ThisIsLegal
Hope this helps!
C11 Standard (N1570, ISO/IEC 9899:201x) (Relevant section: s6.10 Prerocessing Directives, page 160)
Upvotes: 12