C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 1)
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;
}
Output: Have a nice day
No output
Error: Expression syntax
Error: Undeclared identifier if
Answer: Option
Explanation:

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

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

Discussion:
12 comments Page 1 of 2.

Sudhanshu agarwal said:   1 decade ago
@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;
}

Abhayraj SN said:   9 years ago
@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.

Saggs said:   7 years ago
@Juhi.

You must assign value calculated from if(i == 10) && if(j == 20) expression to some variable or put it in another if like if(if(i == 10) && if(j == 20)){/* code goes here*/}.

Sandip Gupta said:   1 decade ago
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.

Juhi said:   9 years ago
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?

Saggs said:   7 years ago
The original question gives output:

main.c: In function 'main':
main.c:5:18: error: expected identifier before 'if',
if(i = 5) && if(j = 10).

Meera said:   1 decade ago
@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.

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

Adil said:   1 decade ago
If we give (i<=5)&&(i<=10) it will become if(FALSE) is it ?

Muni said:   1 decade ago
Output should be "Have a nice day" any valid expression is true.


Post your comments here:

Your comments will be displayed after verification.