Friday, October 30, 2009

Logical programs :

#include < stdio.h >
void main()
{
int a,b;
b=1;
if(a=b);
else
a=2;
a=3;
printf(“%d \t%d “,a,b);
while(b<3)
a+=++b;
printf(“\n %d \t %d”,a,b);
}

What’s the output? Can you answer it ? or it’ll make error?
It’ll successfully run without compilation errors.

Output:

3 1
8 3

How?
At first b=1
As per I said before , it can possible to assign value for variables in if and also if can end with semicolon
if(a=b);
is equivalent to
if(a=b)
{
}

Now a =1
if(a=b) i.e. if(1)
so it won’t go to else part.
So a=3
In while b<3
a+=++b is equivalent to a=a+(++b)
at first b=1 ,a=3
1<3 true so a=3+(2)=5 ,b=2
2<3 true so a=5+(3)=8, b=3
3<3 false
Now a=8 b=3

0 comments:

Post a Comment