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

Manish said:   1 decade ago
Excellent Kavya. !

Gangadhar said:   1 decade ago
You absolutely correct kavya.

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

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.

Dhronan said:   1 decade ago
@ imran and pal:.

There is no problem on executing that statement, because it is terminated at every iteration of for loop. The condition inside the if statement produces 0, but that wouldn't affect the program. Because ";" terminates at every iteration.

Pall said:   1 decade ago
X=10, y=10 , then how if(x != y) statement will execute ?pls some 1 answer asap


Post your comments here:

Your comments will be displayed after verification.