Why this C program outputs a negative number?

Posted by JMSA on Stack Overflow See other posts from Stack Overflow or by JMSA
Published on 2010-05-02T09:25:35Z Indexed on 2010/05/02 9:27 UTC
Read the original article Hit count: 188

Filed under:
|

I have assigned the complement value in an unsigned variable.

Then why this C program outputs a negative number?

#include<stdio.h>
#include<conio.h>

int main()
{
    unsigned int Value = 4;         /*   4 = 0000 0000  0000 0100 */  
    unsigned int result = 0;

    result = ~ Value;               /* -5 = 1111 1111  1111 1011 */  

    printf("result = %d", result);  /* -5             */

    getch();

    return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about unsigned