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.
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)
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)
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.
Kiran said:
1 decade ago
Step1: x and y are intialized to 10 and 10 repectively. 100%90 does not affect on variable Y.
step2: the for loop condition is true and it repeats upto i<10.
Step3: if condition is statisfied in for loop statement block, It will executes without any output upto the condition in for loop is fails.
step4: printf statement is executes independently which is x =10 and y = 10. which is doesn't affect on for loop.
The option 2 and 3 are correct.
step2: the for loop condition is true and it repeats upto i<10.
Step3: if condition is statisfied in for loop statement block, It will executes without any output upto the condition in for loop is fails.
step4: printf statement is executes independently which is x =10 and y = 10. which is doesn't affect on for loop.
The option 2 and 3 are correct.
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.
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?
(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?
Prashant said:
1 decade ago
1. Here x=10, y=10 because 100 % 90 means reminder after dividing 100 by 90 (that is modulo operation). So initialy x=10 and y=10.
2. Here for loop not have curly brases so default scope of for loop is next statement of for loop means for loop contains only one statement that is if(x!=y). So printf statement is not incuded in for loop. As the contol leave the for loop it comes to printf statement and it gets executed.
2. Here for loop not have curly brases so default scope of for loop is next statement of for loop means for loop contains only one statement that is if(x!=y). So printf statement is not incuded in for loop. As the contol leave the for loop it comes to printf statement and it gets executed.
Aahana said:
1 decade ago
Since its given that x=10 and y=100%90 so the value of x will be 10 and value of y will be 10 remainder is considered in case of % operator . and since for loop does not have any scope i.e it does not contain any body so sequential execution will take place and the statement that x!=y is false inspite of that the printf statement will execute because because the condition x!=y is terminated by ;
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)
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers