Tuesday 26 March 2013

j = j || i++ && printf("YOU CAN");


    main()
{
         int i=4,j=7;
    j = j || i++ && printf("YOU CAN");
         printf("%d %d", i, j);
}

Answer:
4 1
Explanation:
The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expression’s truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same.
Similarly when && operator is involved in an expression, when any of the operands become false, the whole expression’s truth value becomes false and hence the remaining expression will not be evaluated.    
    false && (anything) => false where (anything) will not be evaluated.

 Blog Author: Vijay Kumar

No comments:

Post a Comment