This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using macros with variables declared on #define

Any ideas why it's impossible to compile that simple code?

#define AB_VALUES  2,3
#define Add(a,b)  a+b

void main(void)
{
 int c;
 c = Add (AB_VALUES);
}

  • Yep,

    The Add macro expects 2 arguments and you only pass one.

    When you try to do tricky stuff with the preprocessor and you get errors that you can't figure out, you can get the compiler to generate a preprocessor output file (*.i). The option to generate this file is available under the Listing tab in the Project Options dialog.

    Anyway, the following output is generated for your code:

     void main(void)
     {
     int c;
     c = 2,3+ ;
    #error *** ERROR C307 IN LINE 7 OF main.c: macro 'Add': parameter count mismatch
     }
    

    And, it's easy to see why you get the error.

    Jon

  • "get the compiler to generate a preprocessor output file (*.i)."

    Hey! - there you go nicking my Top Tips again:
    http://www.8052.com/forum/read.phtml?id=29152

    BTW: the preprocessor output file (*.i) is still a valid 'C' source file; therefore I always add *.i to the list of 'C' source filetypes for my uVision Projects - so then I can view them with syntax colouring!
    And even compile them, if I want...