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

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. !

Manish said:   1 decade ago
Excellent Kavya. !

Gangadhar said:   1 decade ago
You absolutely correct kavya.

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.

Vishwas said:   1 decade ago
Simple logic is that, in c if anything is terminated by ; then it is treated as normal statement, so in this program the 'if' is treated as normal statement and there is no side effect on the variables, in other words it neglect that statement and proceeds its execution.

(1) As printf is not inside for loop only once the printf get executed....
(2) Compiler would not generate an error, how can it generate an error for simple statement?

Raj said:   1 decade ago
As if is terminated with a semicolon the compiler feel that as end of if statement, so next line printf () is executed in the program.

Abbas haider said:   1 decade ago
Can we put ';' after for loop also?

Dayal said:   1 decade ago
Very good kavya.

Sangeetha said:   1 decade ago
Correct explanation kavya.


Post your comments here:

Your comments will be displayed after verification.