C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a = 300, b, c;
    if(a >= 400)
        b = 300;
    c = 200;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
300, 300, 200
Garbage, 300, 200
300, Garbage, 200
300, 300, Garbage
Answer: Option
Explanation:

Step 1: int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.
Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be failed.
Step 3: c = 200; here variable c is initialized to '200'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.

Discussion:
16 comments Page 1 of 2.

Shivani said:   6 years ago
Thanks all for explaining.

Harshitha said:   7 years ago
Here, after if statement, it means the next statements is also false, but why the below statement is regarded as false (b statement).

Then c=200 will be executed.
(1)

Angshuman said:   8 years ago
How C gets executed if it is inside the loop and loop condition doesn't satisfy?

Abhishek said:   9 years ago
@Deepika.

The default scope of the "if" statement is only the next statement so it won't enter into b statement and go to c statement. If you want to execute more than one statement you need to write it in the pair of braces.

Deepika said:   9 years ago
In this, if(a>=400) then it checks the value and the condition become fails . Then it enters into b statement but why it is going to c statement and the b value becomes garbage why?
(1)

Vikram singh said:   10 years ago
Why C = 200 is independent of "if" statement?

Ramya said:   1 decade ago
Here in the question compiler checks whether 300> = 400 it is false therefore b = 300 is not executed following simple if syntax and it comes out of if and next statement i.e. c = 200 is executed and since storage class of a, b, c is automatic default values of those variables are garbage values since b is not initialized b = garbage and a = 300, c = 200.

Denny said:   1 decade ago
The statement B must be in the braces if not we can think that both B and C are under the 'if statement'. As the condition on the if statement is false both B and C can't be executed.

Abdg said:   1 decade ago
What is Syntax error?

Abhishek said:   1 decade ago
What does b=200 means, and why its not getting executed, why everybody is comparing 200>=300, when its written a>=300.


Post your comments here:

Your comments will be displayed after verification.