C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 6)
6.
Which of the following statements are correct about the below C-program?
#include<stdio.h>
int main()
{
    int x = 10, y = 100%90, i;
    for(i=1; i<10; i++)
    if(x != y);
        printf("x = %d y = %d\n", x, y);
    return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.
1
2, 3
3, 4
4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
70 comments Page 4 of 7.

Prasad said:   1 decade ago
If a condition is followed by semicolon then compiler creates a empty body with {} after if. So it becomes an infinite loop and this loop is repeated till the for loop condition fails.

Santosh SN said:   1 decade ago
Thank you kavya.

Rob said:   1 decade ago
if(x!=y) will not execute, it just checks whether the conditions are matching or nor not. Here in this case the condition is false and if it was true then there might be an error.

1828 said:   1 decade ago
If (x!=y) will execute but compiler will go to next line which is.
'printf ("x = %d why = %d\n", x, y) ;' here X=10 and Y=10 will remain same as they were at declaration time.

Sramona said:   1 decade ago
X=10, y=10, then how if (x != y) statement will execute ? please some one answer ASAP.

Abhay said:   1 decade ago
Thank for best guidance and explanation

Rekha said:   1 decade ago
Here in if(!x<y);
So program will execute sequentially i.e after if(!x<y);statement the control will goes successfully on next line which is printf statement hence it will no produce any error and produce output x=10 and y=10.

Shailendra said:   1 decade ago
Your concept is very clear Kavya

Dana said:   1 decade ago
2,3 comes how?

Aahana said:   1 decade ago
Since its given that x=10 and y=100%90 so the value of x will be 10 and value of y will be 10 remainder is considered in case of % operator . and since for loop does not have any scope i.e it does not contain any body so sequential execution will take place and the statement that x!=y is false inspite of that the printf statement will execute because because the condition x!=y is terminated by ;


Post your comments here:

Your comments will be displayed after verification.