Behaviour of this preprocessor directive is the same for both C and C++ compilers.
Purpose
The #endif directive is used end / close / terminate a selection block (#if, #ifdef, or #ifndef.
Format
#if or #ifdef or #ifndef
preprocessor or code statements
#elif controlling_expression (optional)
preprocessor or code statements
#else (optional)
preprocessor or code statements
#endif
All preprocessor directives begin with the # symbol. It must be the first character on the line or the first character on the line following optional white space.
Some early compilers flagged an error if # was not the first character on the line.
Spaces or tabs are permitted between the # and endif, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and endif together.
If anything follows the #endif directive (other than white space) then the program is malformed.
The following are valid uses:
#endif # endif # /* comments are white space */ endif
The following are invalid uses:
// #\ is not a valid preprocessor directive # \t endif // #" is not a valid preprocessor directive # "" endif // malformed because only white space may follow #endif #endif MY_MACRO
Use
The #endif must appear as the final statement of a preprocessor selection sequence.
#ifdef MY_MACRO
. . . // preprocessor or language statements . . .#endif