# Got problem while using #define preprocessor

#### ep.hobbyiest

Joined Aug 26, 2014
201
While using #define i got problem.
Compiler is not accepting floating values.

my scenario is as follows.

#if volt==2.5 //not working but
#if volt==2 //works,

#### Papabravo

Joined Feb 24, 2006
18,778
#define and #if do not generate executable code as your logic seems to indicate.
#define is used to create text substitution macros that are performed BEFORE the compiler makes its first pass over the source code.
#if, #else, and #endif are used to perform conditional compilation. That is under some conditions you include or don't include a chunk of source code. This all happens BEFORE compilation of the source code begins.

#### spinnaker

Joined Oct 29, 2009
7,835
#define and #if do not generate executable code as your logic seems to indicate.
#define is used to create text substitution macros that are performed BEFORE the compiler makes its first pass over the source code.
#if, #else, and #endif are used to perform conditional compilation. That is under some conditions you include or don't include a chunk of source code. This all happens BEFORE compilation of the source code begins.

To see an example of what PB wrote, just take a look at the header files from microchip or mikro or wherever. They are filled with the #define preprocessor call. The reason is they need to write their code for all kinds of chips. When you build a project with MBLab it defines your pic type for you, so when the code compiles and it sees a #if for your chip, it knows to use that section of the code and ignore the others.

#### ep.hobbyiest

Joined Aug 26, 2014
201
Hi,
I faced this problem while using it. My logic is if volt is 2 then it will define some macro's and if it 2.5 then it will give different values to that macro.
My implementation was like,
Code:
#define Volt 2

#if Volt==2                        //works
#define MULTI 25
#elif Volt==2.5                 // not work
#define MULTI 20
#endif

#### nerdegutta

Joined Dec 15, 2009
2,684
Hi, two thing comes to mind.

1. In line #2 you define Volt as the number two (an integer)
Then in line #6 you check if Volt is equal to two-point-five (float). Is 2 equal to 2.5.

Why do you want to know the voltage at compile time? AFAIK the microcontroller have no idea of what is going on, since it has not been programmed yet.

Do you confuse #if with if?

#### ep.hobbyiest

Joined Aug 26, 2014
201
No, i don't want to know voltage at compiler time.

It is just macro to assign values to different macro.

it is just to configure the code before compiling.

I want to assign different value if values are either 2 or 2.5 instead of commenting out the area.

#### bertus

Joined Apr 5, 2008
21,940
Hello,

Could you use:

#define Volt 2.0
#if Volt==2.0

In that case Volt already would be float

Bertus

#### spinnaker

Joined Oct 29, 2009
7,835
It looks like the preproccessor does not like numbers.

There might be a better way to do this but this works

Code:
#define v V2_5

#if v == V2_0
#define MULTI 25
#elif v == V2_5
#define MULTI 20
#endif
I need to run for an appointment. I will look later to see if there is a way to convert numbers in preprocessor.

Last edited:

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
#if volt==2.5 //not working but
#if volt==2 //works,

2.5 is float value..

#### WBahn

Joined Mar 31, 2012
26,412
While using #define i got problem.
Compiler is not accepting floating values.

my scenario is as follows.

#if volt==2.5 //not working but
#if volt==2 //works,

It looks like the preproccessor does not like numbers.
It likes numbers just fine, as long as they fit the constraints. The expression in a #if must be an integer expression.

https://gcc.gnu.org/onlinedocs/cpp/If.html