#include <stdio.h>
#define M(X,Y) X*Y
void main(){
int a=2,b=3;
printf("%d",M(a+b,a-b));
}
I put this code in photon and got “5”, I just wonder why I didn’t get “-5”.
#include <stdio.h>
#define M(X,Y) X*Y
void main(){
int a=2,b=3;
printf("%d",M(a+b,a-b));
}
I put this code in photon and got “5”, I just wonder why I didn’t get “-5”.
How are you building your code?
Your code doesn’t build for me in Web IDE since it should be int main()
(although you don’t implement a main()
function as this is part of the system itself) and there is no printf()
for the Photon.
If you post code that’s supposed to run on a (stock) Photon, then you should post the exact code you are actually testing.
Can you test again via Web IDE an this code
#define M(X,Y) X*Y
void setup() {
}
void loop() {
int a=2, b=3;
Serial.printlnf("%d", M(a+b,a-b));
delay(1000);
}
and check the output via a serial terminal program?
BTW, if you unpack that macro you’d end up with this code
Serial.printlnf("%d", a+b*a-b);
does this expalin the result?
You may want to rewrite your macro like this
#define M(X,Y) (X)*(Y)
Since M is a macro and not a function, it behaves as a+b*a-b
, or 2+3*2-3
, but since * has precedence over +, it’s grouped as 2+(3*2)-3 = 2 + 6 - 3 = 5.