YAKOVM
YAKOVM

Reputation: 10153

#if /#endif preprocessing directives

Should I always use #if directive coupled with #endif?

  1. If yes - what could be a problem if I only use #if?
  2. If no - when I use only #if, and when combination #if/#else?

Thank you.

Upvotes: 0

Views: 451

Answers (3)

Jay
Jay

Reputation: 24895

1.If yes - what could be a problem if I only use #if?

As mentioned by others if you use only #if and don't match it with a #endif, the preprocessor will fail and it will shown as a compiler error.

2.If no - why I use only #if,and when combination #if/#else?

Even in case of #if/#else, there should be an #endif at the end. Otherwise it will not work. You can try it yourself and check.

These are most commonly used for conditional compilation, apart from checking and defining macros. Please find some relevant links (basic level) below:

http://gcc.gnu.org/onlinedocs/cpp/Ifdef.html
http://en.wikipedia.org/wiki/C_preprocessor
http://www.phanderson.com/C/preprocess.html

Upvotes: 4

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

If you don't have a matching #endif, (or #else ... #endif) your code will fail to compile.

Upvotes: 4

codaddict
codaddict

Reputation: 454930

Yes.

Without the #endif the preprocessor will not come to know the end of the #if block.

Upvotes: 3

Related Questions