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);
}

Parents
  • 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

Reply
  • 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

Children