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 1 of 4.

Nikhil said:   1 decade ago
@Naveen.

Hello, i want to ask one doubt regarding NOT logic operator.
Is !a=0 , for a having value other than zero. Like a=300?

But @levin max
Said that !a>=400 as a<=400. If this is right. then for a=300 the IF condition gets TRUE. and the value b=300 gets assigned, and the output will be displayed as b=300 and c=200.

So please clarify me REGARDING not OPERATOR.

Rohan kumar said:   3 years ago
You explained very well @Nanikanchari.

Here the condition is ;
int a = 500, b = 100, c;
if(!a >= 400)
So let me take only a>=400 okay. and a value is 500 we know;

So, it will be like 500>=400, so it is true. hence value '1' it will consider in the if() condition.
but we have '!' operator in front of it, so the value will be '0'.
(6)

Nanikanchari said:   5 years ago
Here condition is ;
int a = 500, b = 100, c;
if(!a >= 400)
so let me take only a>=400 okay. and a value is 500 we know;

So it will be like 500>=400, so it is true. hence value '1' it will consider in the if() condition.
but we have '!' operator in front of it, so the value will be '0'.

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.

Rohit said:   6 years ago
Here, a is greater than b. But ! Operator is reverse so if condition is false. So after if the value of b is not take in consideration. After b=300; there is SEMICOLON so it terminates if loop. So we get only a and c values. Hope it helps.

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.

Srk said:   1 decade ago
program can be re-written as follows;
#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;
}

Jasson said:   1 decade ago
May be the answer is wrong because there is no begin and end brackets for if loop in that case only first statement executed and second one may be garbage value i.e. in my opinion option B is correct. If I wrong please comment.

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.

Akanksha Reddy said:   8 years ago
The c value is clearly mentioned out from the if loop. Hence as if the condition is failed since it takes only the c value and initial value of b. Hence the option is D.


Post your comments here:

Your comments will be displayed after verification.