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.
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)
Sai said:
1 decade ago
1.Actually after "if" condition there is ;(semicolon) then it becomes an infinite loop.If condition calls itself again and again till termination i.e from 1 to 10.After terminating now control will go to printf.And printf will be called for last checking condition in "if"..
Tabassum said:
1 decade ago
Initailly the value of x=10 and y=100%90 is provided
how u all can say that the value of y is also 10 can one exaplain how its possible becoz it is modulus and that will take only remainder into picture that means it is 1 right or i dont know can any one exaplain me this in more elaboration.
how u all can say that the value of y is also 10 can one exaplain how its possible becoz it is modulus and that will take only remainder into picture that means it is 1 right or i dont know can any one exaplain me this in more elaboration.
Aswin said:
1 decade ago
As We have got a ';' after if condition, it checks for equivalence of x & y for 10 times and executes the ';'.
As ';' in C is treated as a statement thats why every statement ends with a ';'.
and values of x and y are printed as 10 as they are not getting modified any where...
As ';' in C is treated as a statement thats why every statement ends with a ';'.
and values of x and y are printed as 10 as they are not getting modified any where...
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.
Dhronan said:
1 decade ago
@ imran and pal:.
There is no problem on executing that statement, because it is terminated at every iteration of for loop. The condition inside the if statement produces 0, but that wouldn't affect the program. Because ";" terminates at every iteration.
There is no problem on executing that statement, because it is terminated at every iteration of for loop. The condition inside the if statement produces 0, but that wouldn't affect the program. Because ";" terminates at every iteration.
Rekha said:
1 decade ago
Here in if(!x<y);
So program will execute sequentially i.e after if(!x<y);statement the control will goes successfully on next line which is printf statement hence it will no produce any error and produce output x=10 and y=10.
So program will execute sequentially i.e after if(!x<y);statement the control will goes successfully on next line which is printf statement hence it will no produce any error and produce output x=10 and y=10.
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.
Nadeem Khan said:
1 decade ago
Can anybody tell me 100%90 will give remainder or quotient.
Because in my opinion %--> sign is used for getting remainder (Modular sign).
& /--> slash sign is used for dividing.
Because in my opinion %--> sign is used for getting remainder (Modular sign).
& /--> slash sign is used for dividing.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers