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 1 of 7.
Rohan said:
7 years ago
@All.
According to me, it is;
#include<stdio.h>
int main()
{
int x = 10, y = 100%90, i;
/* (i) Intialize x with the value of 10
(ii) Intialize y with the value of 10 [ 100%90 = 10 (% returns remainder)]
(iii) declare i
for(i=1; i<10; i++)
/* There is no relation between for loop and if condition */
if(x != y);
/* (i) [ 10 !=10 ] , the condition is false but
(ii) ";" (semicolon) has terminated the statement and moves to next statement.. */
printf("x = %d y = %d\n", x, y);
/* (i) x, y is assigned the value 10 which has been initialized earlier */
return 0;
}
According to me, it is;
#include<stdio.h>
int main()
{
int x = 10, y = 100%90, i;
/* (i) Intialize x with the value of 10
(ii) Intialize y with the value of 10 [ 100%90 = 10 (% returns remainder)]
(iii) declare i
for(i=1; i<10; i++)
/* There is no relation between for loop and if condition */
if(x != y);
/* (i) [ 10 !=10 ] , the condition is false but
(ii) ";" (semicolon) has terminated the statement and moves to next statement.. */
printf("x = %d y = %d\n", x, y);
/* (i) x, y is assigned the value 10 which has been initialized earlier */
return 0;
}
(12)
Kavya said:
1 decade ago
1. The statement 1 is wrong bcz printf is not inside for loop so printf is called 1 times.
2. Since x = 10 anad y = 10. the value is printed.which is true.
3. We can terminate conditional statement with semicolon, hence if(x!=y); is allowed in c.
4. Since the program is producing o/p, the 4th statement is wrong.
2. Since x = 10 anad y = 10. the value is printed.which is true.
3. We can terminate conditional statement with semicolon, hence if(x!=y); is allowed in c.
4. Since the program is producing o/p, the 4th statement is wrong.
(4)
Sanjay said:
8 years ago
Thank you, for explaining this program.
(2)
Priyanka said:
9 years ago
When this statement executes why = 100% 90 the why value become 10, the loop continue 9 times and the value of if either true or false it's 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 why value as 10; so option B is the correct answer. I agree but how print output 2, 3 please explain someone.
(2)
S.V.KISHORE said:
7 years ago
Good explanation @Kavya.
(1)
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)
Ashish soni said:
10 years ago
Program successfully compile and execute.
It produce output: x = 10; y = 10 for 9 times.
It produce output: x = 10; y = 10 for 9 times.
(1)
Kiranmai said:
9 years ago
@Kavya.
Your explanation is clear and useful. Thank you.
Your explanation is clear and useful. Thank you.
(1)
Antor said:
8 years ago
Explain it, please.
(1)
Saikumar said:
9 years ago
Thank you, @Prasanth.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers