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.
Avijit said:
1 decade ago
When this statement execute y = 100% 90 the y value become 10, the loop continue 9 times and the value of if either true or false its no matter because there is a semicolon after that that means the if end there then it comes to the printf statement and print the x value as 10 and y value as 10; so option B is the correct answer.
Sandeep said:
1 decade ago
Please show me X and Y value correct answer.
Sandeep said:
1 decade ago
X=10, Y=10 for loop statement no inside.
SONIYA VIJAY said:
1 decade ago
x = 10, y = 10.
For loop has no statement inside it.
After the if statement there is a; so no o/p will be produced but it will not give any error.
The printf statement will print the values of x and y.
So the answer is (B) 2, 3.
For loop has no statement inside it.
After the if statement there is a; so no o/p will be produced but it will not give any error.
The printf statement will print the values of x and y.
So the answer is (B) 2, 3.
Hare.. said:
1 decade ago
@Sujan.
In above program we have one for loop is [for(i=1; i<10; i++)], In this for loop 'i' value conditioned as 'i<10'.
If, that 'i' value is using in 'printf' then only we have choice to print that 'i' value. Only 'x' and 'y' values written in printf statement, so we are simply printing x and y values as 10 and 10 respectively.
if(x != y);
printf("x = %d y = %d\n", x, y);
In above two statements if statement is closed with ';' means it will create one anonymous block (shown below) and that if statement is not related to printf statement. We have to treat that Printf statement as independent.
if(x != y); turboc compiler understand this if statement as,
if(x != y)
{
};
So the if statement doesn't have any body part simply it is empty block.
In above program we have one for loop is [for(i=1; i<10; i++)], In this for loop 'i' value conditioned as 'i<10'.
If, that 'i' value is using in 'printf' then only we have choice to print that 'i' value. Only 'x' and 'y' values written in printf statement, so we are simply printing x and y values as 10 and 10 respectively.
if(x != y);
printf("x = %d y = %d\n", x, y);
In above two statements if statement is closed with ';' means it will create one anonymous block (shown below) and that if statement is not related to printf statement. We have to treat that Printf statement as independent.
if(x != y); turboc compiler understand this if statement as,
if(x != y)
{
};
So the if statement doesn't have any body part simply it is empty block.
(1)
Sujan said:
1 decade ago
But the condition says that i<10 rite? So how can we get x and y as 10?
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.
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.
Hare.. said:
1 decade ago
@Deepanshu and @Kavi.
"if (x != y) ;" it will exits and doesn't show you the error in turbo-C compiler.
It is Close with semicolon means that ';' immediately terminates if statement then that if statement immediate braces or block are treated as anonymous block.
"if (x != y) ;" it will exits and doesn't show you the error in turbo-C compiler.
It is Close with semicolon means that ';' immediately terminates if statement then that if statement immediate braces or block are treated as anonymous block.
Deepanshu said:
1 decade ago
If statement cannot be terminated by semi-colon.
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?
Can you justify your answer?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers