Day#23 'C' Coding Challenge

Day#23 'C' Coding Challenge

What is the output of the below code?

include <stdio.h> void main() { int var1 = 4; int var2,var3; (var1 < 40)?printf("%d%d",var2,var3);:var3 = 50; }

---------------------------------------

The above code test the following scenarios:

1. Conditional statement in C language

----------------------------------------

Conditional statement in C has the below format.

Variable = Condition ? Expression 1 : Expression 2;

While the Variable is optional, the Condition and Expression 1 and Expression 2 are must
In place of condition, we have var1<40 which evaluates to TRUE, so, Expression 1 shall be executed.
However, Instead of Expression there is Printf statement and for Expression 2 there is an expression.
So, as both are not expressions, error is thrown.

The below is a valid conditional statement
(var1 < 40)?printf("%d%d",var2,var3):printf("%d%d",var2,var3);

Post a Comment

0 Comments