Humayun Akhtar Humayun Akhtar - 3 months ago 17
C Question

How do i write if conditions like these in macro?

I'm quite new to macro

i know how to use if condition using ternary operator.

How do i convert this into #define

if(i==j)
{
count=count+1;
break;
}


I try my luck with this but seems like it is wrong

#define ifcount(i,j) ((i)==(j)? count=count+1\
break:0)

Answer

Using (abusing) macros to change the flow of the program is usually a bad idea, because the people that will read your code (yourself in a few years) may get surprised by unexpected jumps in the program flow.

That said, your problem has nothing to do with the ternary operator. Actually the ternary operator cannot contain a break in its right side, only expressions.

But macros are allmighty, so you can just do:

#define ifcount(i,j) if ((i)==(j)) { count=count+1; break; } else ;

The else is there to prevent attaching to a nearer unrelated else by accident.