I am confused between the if
/else
and #if
/#else
/#endif
constructs.
Can I ask what's the differences between them?
#if
#else
and #endif
are instructions to the compiler, to only compile the code between them, if a compilation level condition (like a macro being defined or having a certain value) is satisfied.
if
and else
are parts of the compiled algorithm.
What kind of specific situations for me to choose each of them?
The pre-compilation conditions are used to disable parts of the code, in situations where they make no sense (like calls to Windows-specific APIs, when compiling under Linux). They are key to developing cross-platform code (for example).