C Programming - Control Instructions - Discussion

1. 

Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
    int i = 10, j = 20;
    if(i = 5) && if(j = 10)
        printf("Have a nice day");
    return 0;
}

[A]. Output: Have a nice day
[B]. No output
[C]. Error: Expression syntax
[D]. Error: Undeclared identifier if

Answer: Option C

Explanation:

"Expression syntax" error occur in this line if(i = 5) && if(j = 10).

It should be like if((i == 5) && (j == 10)).


Karthi said: (Mar 1, 2011)  
if i give condition
if((i==5)&&(j==10))
means wt will be the answer for this...

Nancy said: (Mar 5, 2011)  
Can two if expressions be combined in the format given above?

Abhishek Rai said: (Jun 17, 2011)  
@ Karthi........ Output: Have a nice day

Muni said: (Apr 12, 2012)  
Output should be "Have a nice day" any valid expression is true.

Meera said: (Jun 27, 2013)  
@Karthi.

If((i==5)&&(j==10)) then expression evaluates to be false because i is not 5 and j is not 10 and hence there will not be any output.

Sudhanshu Agarwal said: (Jul 24, 2013)  
@Meera, @Muni, @Abhishek.

void main()
{
int i=1;
if(i=19)
printf("right");
else
printf("wrong");
}

The above program result produces as "right" because here if(i=19) means if(19)..

But my question is,

Why in the below program if(i=5), if(j=10) not becomes if(5), if(10) respectively.

If (if(5)&& if(10)), the condition becomes true.

int main()
{
int i = 10, j = 20;
if(i = 5) && if(j = 10)
printf("Have a nice day");
return 0;
}

Sandip Gupta said: (Sep 14, 2013)  
If we use assignment operator instead of conditional operator then it will check the value is 0 or non zero number. If it is 0 then condition is false otherwise condition is true.

Adil said: (May 28, 2014)  
If we give (i<=5)&&(i<=10) it will become if(FALSE) is it ?

Abhayraj Sn said: (Sep 8, 2016)  
@Adil,

You are right. It will be False. Since <=, >=, ==, != are conditional operators. So it will check condition.

And "=" operator is just a assigning operator not conditional.

You may Correct or modify me, if required.

Juhi said: (Sep 16, 2016)  
I take condition if(i == 10) && if(j == 20). so error is produced. And error is " expected identifier before if ". How to solve this error?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.