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

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.

Saikumar said:   1 decade ago
Because that x=10 y=1 the valve is printed is true and.

The condition if(x!=y) will not produce any error it is correct.

So 2, 3 options are correct for this above question.

Hare.. said:   1 decade ago
@Angelica.

Here we are using only one for loop, so it isn't necessary to write curly braces. But in case if we use more than one for loop then curly braces are must.

Bala said:   1 decade ago
Initially the value of x=10 and y=100%90 is provided, The condition if(x!=y) then x=10, y=10 then condition is true. So always print the output.

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.

KAPIL DEOL said:   1 decade ago
Because for will execute only 1 statement that's why it will execute only if but 'if' is terminated by ;.

Then it will print 10 10.

Madhu said:   1 decade ago
How can it generate option B. First of all there is an error occurred since there is ";" after 'if'.

Can you justify your answer?

Kavi said:   1 decade ago
If condition does not contain semicolon (;). How its not producing error? any one please explain it.


Post your comments here:

Your comments will be displayed after verification.