C Programming - Control Instructions - Discussion


What will be the output of the program?

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;

[A]. b = 300 c = 200
[B]. b = 100 c = garbage
[C]. b = 300 c = garbage
[D]. b = 100 c = 200

Answer: Option D


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"

Vijaya Zambre said: (Sep 2, 2010)  
b = 300;

Why this step is mentioned there?

Santosh said: (Oct 9, 2010)  
In step 4 if the condition is failed then why it is taking c=200.

Srk said: (Jan 3, 2011)  
program can be re-written as follows;
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;

Vino said: (Mar 13, 2011)  
I want the meaning of if(!a>=400).

Naveen said: (Jul 1, 2011)  
! is having highest preference
so !500 is 0
>= is next to ! in preference
so 0 >= 400 is 0(flase)

Levin Max said: (Sep 14, 2011)  
Take !a>=400 as a<=400.

Venkat said: (Oct 2, 2011)  
What does this symbol "!" in the above program? can any one explain clearly?

Ganga said: (Jan 2, 2012)  
Why b value gets 100 any one explain me?

Suneetha said: (Feb 6, 2012)  
Because the conidition is failed. So we already assigend the value b=100 so it will print.

Biswajit said: (Mar 29, 2012)  

Your explanation is right, "!" it check the value whether it is zero or not, if Zero it return 1 otherwise 0.

Nikhil said: (Aug 14, 2012)  

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.

Botta said: (Aug 27, 2012)  
you are not supposed to take your own values to 'a' since 500 is already assigned to it.

Jasson said: (Jul 15, 2013)  
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.

Ayyappa said: (Aug 11, 2013)  
'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.

Soumya said: (Nov 16, 2013)  
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.

Meet123 said: (Dec 25, 2013)  
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.

Mayank Bhardwaj said: (Mar 9, 2014)  
Yes because when we don't uses braces then if statement take only first statement.

X-Hedow said: (Oct 28, 2014)  
! 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.

Bhaskar said: (Oct 31, 2014)  
What is the difference between !(a<=40) and (!a<=40)?

Ratna said: (Jan 18, 2015)  
When there is no body part, loop ended at the immediate semicolon.

Denny said: (May 18, 2015)  
What is meant by 500?

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

Mahalakshmi said: (Sep 13, 2015)  
How !500=0? Please explain.

Sk Shadab said: (Mar 6, 2016)  
Here !a means not a, means a is false. And in c false means 0. So !a==0.

Surabhi Agarwal said: (Oct 21, 2016)  
Why are we assigning c = 200 when if the condition fails?

Akanksha Reddy said: (Jul 16, 2017)  
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.

Vineetha said: (Jan 28, 2018)  
Than why mention b=300 here?

Akshay said: (Jun 22, 2018)  
As per my knowledge, it is;

int a=50, b,c;
printf ("%d %d ",b,c);

Rohit said: (Mar 4, 2019)  
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.

Nanikanchari said: (Apr 9, 2020)  
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'.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.