I'm trying to make a macro with the following formula: (a^2/(a+b))*b
, and I want to make sure that the there will be no dividing by zero.
#define SUM_A( x, y ) if( x == 0 || y == 0) { 0 } else { ( ( ( x * x ) / ( ( x ) + ( y ) ) ) * ( y ) )}
and then I call the macro inside main:
float a = 40, b = 10, result; result = SUM_A(a, b); printf("%f", result);
I've tried using brackets around the if function but I keep getting syntax errors before the if statement. I've also tried using return, but I read somewhere that you're not supposed to use that in define.
You can place or nest an If statement inside another If statement. When you nest If statements, you can check for a condition only when another condition is found to be true.
This is perfectly legal - it simply defines the function within the if statement block.
Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
The if (and by extension, elif ) statement runs the block if the given expression is true. If you want to check if something isn't true, you reverse the logic (putting a not in front of it, or using the inverse operator).
You can not use if statement, because #define
is interpret by the preprocessor, and the output would be
result=if( x == 0 || y == 0) { 0 } else { ( ( ( x * x ) / ( ( x ) + ( y ) ) ) * ( y ) )}
which is wrong syntax.
But an alternative is to use ternary operator. Change your define to
#define SUM_A( x, y ) ((x) == 0 || (y) == 0 ? 0 : ( ( ( (x) * (x) ) / ( ( x ) + ( y ) ) ) * ( y ) ))
Remember to always put your define between parentheses, to avoid syntax error when replacing.
As far as I know, what you're trying to do (use if
statement and then return a value from a macro) isn't possible in ISO C... but it is somewhat possible with statement expressions (GNU extension).
Since #define
s are essentially just fancy text find-and-replace, you have to be really careful about how they're expanded. I've found that this works on gcc
and clang
by default:
#define SUM_A(x, y) \ ({ \ float answer; \ if ((x) == 0 || (y) == 0) { \ answer = 0; \ } else { \ answer = ((float)((x)*(x)) / ((x)+(y))) * (y); \ } \ answer; \ }) // Typecasting to float necessary, since int/int == int in C
Brief explanation of the things in this macro:
\
at the end of each line is to signal line continuation (i.e. to tell the compiler "this macro continues on the next line")({
is a statement expression (GNU extension; not part of standard C).x
was 2+1
, then (x)*(x)
would expand to (2+1)*(2+1)
, which is 9 (what we wanted), but x*x
would expand to 2+1*2+1
, which is 5 (not what we wanted)return
value (hence the answer;
at the end)This should give you the result you're looking for, and there's no reason it can't be extended to include multiple else if
s as well (though as other answers have pointed out, it's probably better to use the ternary operator if you can).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With