简体   繁体   中英

how to link plus or minus sign in c macro?

my code is:

#define CHECK(x) if((x)==100)printf("%s = 100\n",#x);
#define C9(x) CHECK(x##9);CHECK(x##PLUS##9);CHECK(x##MINUS##9);
#define PLUS +
#define MINUS -

int main(){
 C9(123-45-67+8);
 return 0;
}

the macro expand to this(I add some \\n to make code readable):

int main(){
 if((123-45-67+89)==100)printf("%s = 100\n","123-45-67+89");;
 if((123-45-67+8PLUS9)==100)printf("%s = 100\n","123-45-67+8PLUS9");;
 if((123-45-67+8MINUS9)==100)printf("%s = 100\n","123-45-67+8MINUS9");;;
 return 0;
}

And it is expected to expand to this :

int main(){
 if((123-45-67+89)==100)printf("%s = 100\n","123-45-67+89");;
 if((123-45-67+8+9)==100)printf("%s = 100\n","123-45-67+8+9");;
 if((123-45-67+8-9)==100)printf("%s = 100\n","123-45-67+8-9");;;
 return 0;
}

How to do it ? Thanks

Firstly, you should define PLUS and MINUS prior to using. secondly I think you do not want to use ## around PLUS and MINUS as so:

#define CHECK(x) if((x)==100)printf("%s = 100\n",#x);
#define PLUS +
#define MINUS -
#define C9(x) CHECK(x##9);CHECK(x PLUS 9);CHECK(x MINUS 9);

int main(){
 C9(123-45-67+8);
 return 0;
}

Oh,I am so fool

this code work

#define C9(x) CHECK(x##9);CHECK(x+9);CHECK(x-9);

it is easy =_=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM