Question

code is:

#include<stdio.h>
int main()
{
    signed x,a;
    unsigned y,b;

    a=(signed)10u;
    b=(unsigned)-10;

    y = (signed)10u + (unsigned)-10;
    x = y;

    printf("a=%d, b=%u",a,b);

    if(x==y)
        printf("\nx=%d, y=%d",x,y);
    else if(x!=y)
        printf("\nx=%u, y=%u",x,y);

    return 0;
}

Output is:

a=10, b=65526
x=0, y=0

Please explain this output. I am working on Turbo C.

Was it helpful?

Solution

y = 10 + 65526 = 65536 = 0 (mod 16-bits).

You have to remember that at the end of the day, signed or unsigned, it is just bits being assigned to a memory location. How they are interpreted can be a matter or context. So writing -10 to an unsigned variable is the same as writing 65526 to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top