Can #if pre-processor directives be nested in C ++?

I have a question about preprocessor directives in C ++:

For example:

#ifndef QUESTION //some code here #ifndef QUESTION //some code here #endif #endif 

Can we use it that way and can the C ++ compiler match ifndef and endif right way?

+60
c ++ c-preprocessor preprocessor-directive
Jul 13 '11 at 11:52
source share
3 answers

Yes we can. The #endif operator matches the previous #if #ifdef or #ifndef , etc., for which there was no corresponding #endif .

eg.

 #if ----------| #if -----| | #endif ---| | #endif --------| 
+80
Jul 13 2018-11-11T00:
source share
— -

Yes, you can #if / #endif blocks. Some styles of C-style will tell you to write

 #ifdef CONDITION1 # ifdef CONDITION2 # endif #endif 

using spaces to indicate the level of nesting.

+34
Jul 13 '11 at 11:55
source share

In your code, the #ifndef QUESTION section will be dropped if you are not #UNef QUESTION.

Good luck

0
Jul 13 '11 at 11:59
source share



All Articles