Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a if statement inside #define?

Tags:

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.

like image 597
Frey1337 Avatar asked Oct 20 '12 14:10

Frey1337


People also ask

Can you put an if inside an if?

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.

Can you put a function inside an IF statement?

This is perfectly legal - it simply defines the function within the if statement block.

Can we use if inside if in Java?

Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

Can we use if inside Elif?

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).


2 Answers

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.

like image 181
tomahh Avatar answered Oct 14 '22 07:10

tomahh


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 #defines 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:

  • The \ at the end of each line is to signal line continuation (i.e. to tell the compiler "this macro continues on the next line")
  • The ({ is a statement expression (GNU extension; not part of standard C).
  • Though not strictly necessary, it's safer to wrap up each use of the parameter/s in parentheses to avoid operator-precedence gotchas. For example, if 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)
  • In statement expressions, the last line functions like the 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 ifs as well (though as other answers have pointed out, it's probably better to use the ternary operator if you can).

like image 27
Saeed Baig Avatar answered Oct 14 '22 07:10

Saeed Baig