C Programming - Expressions - Discussion

Discussion Forum : Expressions - Yes / No Questions (Q.No. 1)
1.
Are the following two statement same?
1. a <= 20 ? (b = 30): (c = 30);
2. (a <=20) ? b : (c = 30);
Yes
No
Answer: Option
Explanation:

No, the expressions 1 and 2 are not same.

1. a <= 20 ? (b = 30) : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    b = 30;
}
else
{
    c = 30;
}

2. (a <=20) ? b : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    //Nothing here
}
else
{
    c = 30;
}

Discussion:
3 comments Page 1 of 1.

Jayesh said:   9 years ago
@Vydehi.

a< = 20.
It works without parenthesis.

Vydehi said:   10 years ago
1. a <= 20 ? (b = 30):(c = 30);

In above there should be parenthesis for condition((a<=20)). Otherwise it is syntactically wrong.

Sundar said:   1 decade ago
Let me give an example to the above problem.

#include<stdio.h>
int main()
{

int a,b,c;

a = 11, b = 22 , c = 33;
a <= 20 ? (b = 30): (c = 30);
printf("\n b = %d, c = %d", b, c);


a = 11, b = 22 , c = 33;
(a <=20) ? b : (c = 30);
printf("\n b = %d, c = %d", b, c);

return 0;
}

// ouput:
b = 30, c = 33
b = 22, c = 33

Therefore, the given statements are not doing the same thing.

Post your comments here:

Your comments will be displayed after verification.