Monday 11 March 2013

Signed Unsigned Logic


    #include<stdio.h>
    int main()
{
    signed x;
    unsigned y;
    x = 10 +- 10u + 10u +- 10;
    y = x;
    if(x==y)
         printf("%d %d",x,y);
    else if(x!=y)
         printf("%u  %u",x,y);
    return 0;
}
Output: 0  0
Explanation: x = 10 +- 10u + 10u +- 10;
10: It is signed integer constant.
10u: It is unsigned integer constant.
X: It is signed integer variable.
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type. Signed is higher data type than unsigned int. So our expression is:
x = 10 + (-10u) + 10u + (-10);
  = 10 + -10 + 10 + (-10);
  = 0


Blog Author: Vijay Kumar

Go to: Java Aptitude

1 comment: