Got problem while using #define preprocessor

Discussion in 'Programmer's Corner' started by ep.hobbyiest, Dec 28, 2015.

  1. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    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,
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,144
    1,791
    #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.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012

    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.
     
  4. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    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 (Text):
    1.  
    2. #define Volt 2
    3.  
    4. #if Volt==2                        //works
    5. #define MULTI 25
    6. #elif Volt==2.5                 // not work
    7. #define MULTI 20
    8. #endif
    9.  
    10.  
     
  5. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    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?
     
  6. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    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.
     
  7. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,347
    Hello,

    Could you use:

    #define Volt 2.0
    #if Volt==2.0

    In that case Volt already would be float

    Bertus
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    It looks like the preproccessor does not like numbers.

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

    Code (Text):
    1.  
    2. #define v V2_5
    3.  
    4. #if v == V2_0
    5.   #define MULTI 25
    6. #elif v == V2_5
    7.   #define MULTI 20
    8. #endif
    9.  
    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: Dec 31, 2015
  9. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    #if volt==2.5 //not working but
    #if volt==2 //works,

    2.5 is float value..
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,748
    4,796

    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
     
Loading...