user1697752
user1697752

Reputation: 9

Difference between using an if statement vs a #if statment

I was wondering if using an if statement vs a #if and #ifdef statement was a matter of style or are there differences in functionality and why you would use one over the others.

Is this the correct way yo use a #ifdef statement?

#define variable 1

#ifdef variable      //if variable is defined as 1?
//code
#endif

Upvotes: 0

Views: 238

Answers (8)

Mike
Mike

Reputation: 49483

There is an important reason for this distinction:

Code 1 -

#if SOMETHING
    do_some_c_stuff();
#endif
  1. If "SOMETHING" is true, we'll call the do_stuff function
  2. If "SOMETHING" is false, this code won't be compiled.
  3. If "SOMETHING" is not defined in your code base, not be compiled.

Code 2 -

#ifdef SOMETHING
    do_some_c_stuff();
#endif
  1. If "SOMETHING" is defined (true or false) we'll call the do_some function
  2. If "SOMETHING" is not defined in your code base, this code won't be compiled

That's why you'll frequently see:

#ifdef SOMETHING
#if SOMETHING
    do_some_c_stuff();
#endif
#endif

or some combination thereof

Upvotes: 1

Lundin
Lundin

Reputation: 215115

#if and #ifdef are evaluated at compile time, they are pre-processor instructions. The item you have named "variable" in your code is not a variable, but a numeric constant that will replace all occurrences of "variable" in the code with 1, before the code is even compiled. All such pre-processor instructions are just indications to the compiler for how it should translate your code into a binary.

An ordinary if-statement is something entirely different, it is part of the actual program itself and performs runtime checks and thereby controls the "flow" of the program.

Upvotes: 0

Stephen Smally
Stephen Smally

Reputation: 66

#ifdef is preprocessed by the compiler, this mean that the expression is not evaluated at runtime. It's usually used to check for a compile flag.

Upvotes: 0

MOHAMED
MOHAMED

Reputation: 43598

all line which us the # are treated by the compiler (gcc). and not treated in the binary (runtime)

Is this the correct way yo use a #ifdef statement? --> yes

Upvotes: 0

Gromer
Gromer

Reputation: 9931

http://msdn.microsoft.com/en-us/library/4y6tbswk(v=vs.100).aspx

Cliffs: It's evaluating the #if at compile time, and based on the result, it does the code in the block of code inside of the #if...#endif. Common use:

#if DEBUG
    // Do some additional logging here we don't want to to in release.
#endif

As was already said, you don't have access to local variables, the preprocessor directives access things you've defined. By default in Visual Studio, DEBUG is defined when you have your mode set to Debug. If you add that #if DEBUG` code from above to a project, you'll notice the code inside of it will "grey" out a bit when you switch to Release mode (assuming all default settings). This is Visual Studio saying that the greyed out code won't be run in the current configuration when you compile.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

The two statement are executed by two different stages of the compiler:

#ifdef ...

is executed by the preprocessor. It removes or leaves intact its guarded portion of code. This is done at compile time, therefore the condition must be a compile-time constant. If a part of the code is excluded by #ifdef, the corresponding code does not go into the compiled executable.

if (...)

is processed by the compiler, and gets converted into executable code. Its expression can be computed at run-time. Both sides of the expression remain in the compiled executable*.


* Unless an optimizer detects that one part can be removed; this is not common.

Upvotes: 1

hmjd
hmjd

Reputation: 122011

#if is used by the preprocessor and is used to conditionally include sections of code for compilation. A typical use is when a source file must be compiled for different operating systems (or different compilers):

#ifdef _WIN32
/* Something specific to windows. */
#else
/* Something not available on windows. */
#endif

An if statement is used to control the flow of a program during runtime.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409472

All directives prefixed by # are handled by a part of the compiler that runs before the compilation, called the preprocessor. That is, for the code in your example if variable is not defined the code between the #ifdef and the #endif will not even be seen by the compiler.

Upvotes: 1

Related Questions