Monday, 19 August 2013

C++ Tricky Program 1

What could be Output of Program given below :-


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=500,b=100,c;
if (!a>=400)
b=300 ;
c=200;
printf("Value of b is :- %d \n Value of c is :- %d",b,c);

}

OUTPUT :-




Explanation To Program :-


if (!a>=400)b=300 ;c=200;


Here first it is checked that (!a>=400) or (!500>=400)


so its true 500 is greater than 400 so it returns true but we have NOT sign in front 

so true returned by condition turns into false.

Now we are not having brackets after if condition if we don't have brackets around them then only next line after if condition is regarded as true statement and upnext statement is else statement 
So as condition finally returned a false so upnext statement c= 200 ; will be executed

Share this

2 Responses to "C++ Tricky Program 1"

  1. Can any one explain how this o/p came?

    ReplyDelete
  2. Explanation To Program :-

    if (!a>=400)
    b=300 ;
    c=200;

    Here first it is checked that (!a>=400) or (!500>=400)
    so its true 500 is greater than 400 so it returns true but we have NOT sign in front so true returned by condition turns into false.

    Now we are not having brackets after if condition if we don't have brackets around them then only next line after if condition is regarded as true statement and upnext statement is else statement

    So as condition finally returned a false so upnext statement c= 200 ; will be executed

    ReplyDelete