Issue in Conditional Operator In C

I am trying to use the conditional operator in C to check if a number is even or odd, but I keep getting an error when I compile my code. Here is what I have written:

if (num % 2 == 0 ? printf("Even") : printf("Odd"))

Can someone please help me understand what I am doing wrong and how to fix it? I would really appreciate any guidance on this.

You’re using 2 different types of conditions in there.

You’ve got your standard if/else

if (condition) { executeWhenTrue } else { executeWhenFalse }

But then you’ve also got ternary condition:

(condition) ? executeWhenTrue : executeWhenFlase;

You’ve tried to use them both at the same time, choose just one! :slightly_smiling_face:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.