Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#elseif vs #elif (C/C++ preprocessor)

I have found that writing

#ifdef ...
#elseif defined(...)
#else
#endif

always results in using either the #ifdef or the #else condition, never the #elseif. But substituting #elif causes it to work as expected based on what's defined. What convoluted purpose, if any, is served by the existence of #elseif? And if none, why doesn't the preprocessor complain?

Maybe this is why for years (decades, really), I've been using ugly #else/#endif blocks, since at least they're reliable!

like image 937
Jerry Miller Avatar asked Mar 21 '16 17:03

Jerry Miller


1 Answers

#elseif is not defined. The preprocessor doesn't complain because your #ifdef is false, and the directives within that #ifdef block are not parsed. To illustrate it, this code is valid:

#if 0
#random nonsense
#else
// This must be valid
#endif
like image 91
Yexo Avatar answered Oct 17 '22 19:10

Yexo