C Programming - Control Instructions - Discussion

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

Initially variables a = 500, b = 100 and c is not assigned.

Step 1: if(!a >= 400)
Step 2: if(!500 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.
Step 5: So, variable c is assigned to a value '200'.
Step 6: printf("b = %d c = %d\n", b, c); It prints value of b and c.
Hence the output is "b = 100 c = 200"

Discussion:
33 comments Page 2 of 4.

Mahalakshmi said:   10 years ago
How !500=0? Please explain.

Jinal said:   1 decade ago
Not understand what meaning if (!a>=400) please explain it.

Denny said:   1 decade ago
What is meant by 500?

Ratna said:   1 decade ago
When there is no body part, loop ended at the immediate semicolon.

Bhaskar said:   1 decade ago
What is the difference between !(a<=40) and (!a<=40)?

X-hedow said:   1 decade ago
! means not equal to which in the case is 500.

Then I think we should start considering 'b' and 'c'.

Therefore 'b' is true.

But as for 'c' I do not understand where it's value came from.

Mayank bhardwaj said:   1 decade ago
Yes because when we don't uses braces then if statement take only first statement.

Meet123 said:   1 decade ago
Actually if statement execute the statement which immediately follows it conditional statement.

So in this case b=300. Which will be either executed or not executed whether condition is true or false. So condition is false and immediate condition is not executed.

Soumya said:   1 decade ago
I am having a doubt with how C is getting the value of 200 even if the condition is false and we don't know the position of C whether it is outside the loop or not.

Ayyappa said:   1 decade ago
'b' takes up the initial value i.e. 100 since the condition (!a>=400) fails. You take that condition to be (a<400) but the value of a is 500 so the condition is false and then 'c' is placed out of the if loop so it takes up 200.


Post your comments here:

Your comments will be displayed after verification.