codiac
codiac

Reputation: 1991

Is it evaluated by preprocessor before runtime?

I have done this code in order for it to be evaluated by the preprocessor before runtime.

#define setPinToPortBit(pin)\
    if (pin < 19) PORTD ## pin;\
    else if(pin>14) PORTB ## (pin-14)\
        else PORTC ## (pin - 8)

#define STATION1 setPinToPortBit(15)

Is there any way to know when will this code sample be evaluated?

Is it evaluated at preprocessing time such that the code digitalWrite(STATION1, 1) will result, before compilation, in digitalWrite(PORTB1, 1) or some expanded thing which include the ifs and elses like digitalWrite(if(15<19)....?

Upvotes: 0

Views: 1126

Answers (4)

Nick Louloudakis
Nick Louloudakis

Reputation: 6015

If you ask whether or not the preprocessor will make the variable check and then apply the right definition for you, the answer is NO. The sequence is that preprocessor directives will become evaluated & replaced by the preprocessor before the compiler takes a job in, therefore the preprocessor has no idea of what the value of the normal variables is, and can not make the check for you.

What the preprocessor will do though, is find where the directives are used and just replace it with your code snippet.

Upvotes: 0

Sridhar Nagarajan
Sridhar Nagarajan

Reputation: 1105

the macros are expanded by preprocessor before running the compiler proper.

the expanded version of your code will look like,

digitalWrite( if (15 < 19) PORTD15; else if(15>14) PORTB (15-14) else PORTC (15 - 8), 1)

you can look at the preprocessed output by using gcc -E file.c. It will stop after the preprocessing stage. The output is in the form of preprocessed source code, which is sent to the standard output. Input files that don't require preprocessing are ignored.

There are three general reasons to use a conditional.

  1. A program may need to use different code depending on the machine or operating system it is to run on. In some cases the code for one operating system may be erroneous on another operating system; for example, it might refer to data types or constants that do not exist on the other system. When this happens, it is not enough to avoid executing the invalid code. Its mere presence will cause the compiler to reject the program. With a preprocessing conditional, the offending code can be effectively excised from the program when it is not valid.
  2. You may want to be able to compile the same source file into two different programs. One version might make frequent time-consuming consistency checks on its intermediate data, or print the values of those data for debugging, and the other not.
  3. A conditional whose condition is always false is one way to exclude code from the program but keep it as a sort of comment for future reference.

Simple programs that do not need system-specific logic or complex debugging hooks generally will not need to use preprocessing conditionals.

The conditional directives are:

#ifdef - If this macro is defined
#ifndef - If this macro is not defined
#if - Test if a compile time condition is true
#else - The alternative for #if
#elif - #else an #if in one statement
#endif - End preprocessor conditional

examples:

  #ifdef DEBUG
    /* Your debugging statements here */
  #endif

#if((NUM%2)==0)
      printf("\nNumber is Even");
#else
      printf("\nNumber is Odd");
#endif

Upvotes: 1

Giulio Franco
Giulio Franco

Reputation: 3230

Assume you have the line

digitalWrite(STATION1, 1)

The preprocessor will turn it into (given I can still do maths):

digitalWrite(if (15 < 19) PORTD15; else if(15>14) PORTB1 else PORTC7)

Now, to my knowledge, this doesn't compile, because if is a statement of type void, meaning that it doesn't yield a value. To make things straight, all you need to do is

#define setPinToPortBit(pin) \
    pin < 19 \
        ? PORTD ## pin \
        : pin > 14 \
            ? PORTB ## (pin-14) \
            : PORTC ## (pin - 8)

Now, the preprocessor will turn the line into

digitalWrite(15<19 ? PORTD15 : 15>14 ? PORTB1 : PORTC7);

This is correct C, if PORTD15, PORTB1 and PORTC7 are valid constants with the same static type.

Now, the C compiler will compile this. By the standard, the compiler is authorized to compile this code as if it was

digitalWrite(PORTD15);

But this is not compulsory: the compiler can choose, based on whichever thing it wants.

What I've usually seen is that all decent modern compilers can and will pre-evaluate these constant conditions, if you enable optimizations. If optimizations are not enabled or you're compiling in debug mode, then I think the compiler will keep the statement as it is, without pre-evaluating the conditions. Because you might want to step into them and to change the natural execution via the debugger, and then you might want that code to be compiled and available, even though it will never normally be executed.

Of course, you can have much more accurate information by disassembling your executable and analyzing it.

Upvotes: 1

MichaelCMS
MichaelCMS

Reputation: 4763

It depends on what you mean by evaluation.

Defines are expanded (in other words copy pasted) through the code by the preprocessor.

Evaluation of constants and constant expressions will be done at compile time. The if clauses, if they turn to if (true) or if (false) will be evaluated at compile time and the compiler might decide to remove unreachable code.

If you want if clauses evaluated by the preprocessor there is #if , #elif or #else .

Upvotes: 0

Related Questions