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. |
Discussion:
70 comments Page 2 of 7.
Kavi said:
1 decade ago
If condition does not contain semicolon (;). How its not producing error? any one please explain it.
Sramona said:
1 decade ago
X=10, y=10, then how if (x != y) statement will execute ? please some one answer ASAP.
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.
'printf ("x = %d why = %d\n", x, y) ;' here X=10 and Y=10 will remain same as they were at declaration time.
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.
Santosh SN said:
1 decade ago
Thank you kavya.
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.
Rajsri said:
1 decade ago
The printf() statement is under for loop or if condition?
If it is under for loop, the explanation is correct but as per the spaces given in the code, it looks so that it is under the if() , if it is under the if condition as ; terminates the if, the compiler must never go to the printf().
Am I missing something? Someone please explain?
If it is under for loop, the explanation is correct but as per the spaces given in the code, it looks so that it is under the if() , if it is under the if condition as ; terminates the if, the compiler must never go to the printf().
Am I missing something? Someone please explain?
Mohan said:
1 decade ago
@Rajsri.
As per your point it looks good, but the spaces given below the loop and starts with Printf() statement doesn't mean that it included the for statement it's just an indentation we are following for avoiding the confusion.. Since ; (semicolon)which is at the end of IF statement which will not generate the error but it keeps looping until the statement becomes false,once it becomes false it comes to the next line and continues its processing.
As per your point it looks good, but the spaces given below the loop and starts with Printf() statement doesn't mean that it included the for statement it's just an indentation we are following for avoiding the confusion.. Since ; (semicolon)which is at the end of IF statement which will not generate the error but it keeps looping until the statement becomes false,once it becomes false it comes to the next line and continues its processing.
Amit said:
1 decade ago
B is true.
As in the above Code,
Till the For statement, the flow is normal & variable values are(x=10, y=100%90=10)
So after the For loop The if Condition is Checked i.e.
if(x!=y)i.e (10!=10)
Which proves to be false , it puts us in dilemma that what will be the output.
But thanks to the ; So that if loop gets terminated here only.
And Controls moves to the next line & values of X & Y gets printed i.e 10 & 10.
As in the above Code,
Till the For statement, the flow is normal & variable values are(x=10, y=100%90=10)
So after the For loop The if Condition is Checked i.e.
if(x!=y)i.e (10!=10)
Which proves to be false , it puts us in dilemma that what will be the output.
But thanks to the ; So that if loop gets terminated here only.
And Controls moves to the next line & values of X & Y gets printed i.e 10 & 10.
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.
Then it will print 10 10.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers