Monday 11 March 2013

Corresponding Value of Signed Unsigned


    #include<stdio.h>
    int main()
{
    int a=-5;
    unsigned int b=-5u;
    if(a==b)
         printf("Avatar");
    else
         printf("Alien");
    return 0;
}
Output: Avatar
Explanation: int a=-5;
Here variable a is by default signed int.
unsigned int b=-5u;
Constant -5u will convert into unsigned int. Its corresponding unsigned int value will be :
65536 – 5 + 1= 65532
So, b = 65532

In any binary operation of dissimilar data type for example: a == b
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.
In c signed int is higher data type than unsigned int. So variable ‘b’ will automatically type casted into signed int.
So corresponding signed value of 65532 is -5
Hence, a==b


Blog Author: Vijay Kumar

Go to: Java Aptitude

No comments:

Post a Comment