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.
1
2, 3
3, 4
4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
70 comments Page 1 of 7.

S.V.KISHORE said:   7 years ago
Good explanation @Kavya.
(1)

Vivek kumar said:   7 years ago
Thanks @Kavya.

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;
}
(12)

Antor said:   8 years ago
Explain it, please.
(1)

Sanjay said:   8 years ago
Thank you, for explaining this program.
(2)

Saikumar said:   9 years ago
Thank you, @Prasanth.
(1)

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)

Kiranmai said:   9 years ago
@Kavya.

Your explanation is clear and useful. Thank you.
(1)

AVI said:   9 years ago
I don't now after if condition put semicolon in C you clearly explain.

Ashish soni said:   10 years ago
Program successfully compile and execute.

It produce output: x = 10; y = 10 for 9 times.
(1)


Post your comments here:

Your comments will be displayed after verification.