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 6 of 7.

Keerthana Sundaram said:   1 decade ago
Clear explanation. Thanks.

Sai said:   1 decade ago
1.Actually after "if" condition there is ;(semicolon) then it becomes an infinite loop.If condition calls itself again and again till termination i.e from 1 to 10.After terminating now control will go to printf.And printf will be called for last checking condition in "if"..

Aswin said:   1 decade ago
As We have got a ';' after if condition, it checks for equivalence of x & y for 10 times and executes the ';'.
As ';' in C is treated as a statement thats why every statement ends with a ';'.
and values of x and y are printed as 10 as they are not getting modified any where...

Prashant said:   1 decade ago
1. Here x=10, y=10 because 100 % 90 means reminder after dividing 100 by 90 (that is modulo operation). So initialy x=10 and y=10.

2. Here for loop not have curly brases so default scope of for loop is next statement of for loop means for loop contains only one statement that is if(x!=y). So printf statement is not incuded in for loop. As the contol leave the for loop it comes to printf statement and it gets executed.

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 ;

Tabassum said:   1 decade ago
Initailly the value of x=10 and y=100%90 is provided
how u all can say that the value of y is also 10 can one exaplain how its possible becoz it is modulus and that will take only remainder into picture that means it is 1 right or i dont know can any one exaplain me this in more elaboration.

Bhushan said:   1 decade ago
Thanks kavya. !


Post your comments here:

Your comments will be displayed after verification.