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.

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.

Sonal Sharma said:   3 years ago
if(!r)
p=!r?100:200;
else
p=r?300:400;

What will be the value of p when r is true? and when r is false?

Can anyone explain it to me?

Biswajit said:   1 decade ago
@Naveen.

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

AKSHAY said:   7 years ago
As per my knowledge, it is;

int a=50, b,c;
if(a<=40)
b=100;
C=200;
printf ("%d %d ",b,c);
(1)

Naveen said:   1 decade ago
! is having highest preference
so !500 is 0
>= is next to ! in preference
so 0 >= 400 is 0(flase)
;)

Botta said:   1 decade ago
@Nikhil
you are not supposed to take your own values to 'a' since 500 is already assigned to it.

Suneetha said:   1 decade ago
Because the conidition is failed. So we already assigend the value b=100 so it will print.

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

Venkat said:   1 decade ago
What does this symbol "!" in the above program? can any one explain clearly?

Sk shadab said:   9 years ago
Here !a means not a, means a is false. And in c false means 0. So !a==0.


Post your comments here:

Your comments will be displayed after verification.