Where is the preprocessor directive legal?

I am trying to write a state machine that breaks the source file and breaks it into sections, which are either the compiler or the preprocessor business. Not a deep workaround, I'm just looking for sections that are either comments or preprocessor directives. (without macros, without conditionally compiled blocks, etc.)

The comments are quite simple, but I'm not 100% sure when it is legal to specify a preprocessor directive. For example, does the following line match?

int i; #include <derp.h> 

Are there any special cases where some directives are allowed while others are not?

I searched google and SO and did not find a question that answers this.

Please be responsible for BOTH C and C ++, I noted both intentionally and intentionally.

+6
source share
1 answer

Preprocessor directives can appear anywhere as long as they are the first non-skip marker in a string. Accordingly, you cannot write

 int i; #define ThisIsntLegal SinceItsNotAtTheStart 

But it will be:

 int i; #define Woohoo ThisIsLegal 

Hope this helps!

Standard C11 (N1570, ISO / IEC 9899: 201x) (Relevant section: s6.10 of the Prerocessing Directive, p. 160)

+11
source

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


All Articles