Why unsigned int contained negative number

前端 未结 4 2281
谎友^
谎友^ 2020-12-20 16:38

What I know about unsigned numerics (unsigned short, int and longs), that It contains positive numbers only, but the following simple program successfully assigned a negativ

4条回答
  •  青春惊慌失措
    2020-12-20 17:04

    With printf, the %i format outputs a signed int. Use %u to output an unsigned int. This is a common issue when beginning C programming. To address your question, the result of v1 - v2 is -10, but sum is an unsigned int, so the real answer is probably something like 4294967286 (232 - 10). See what you get when you use The subtraction of %i from %i is %u \n. :)

提交回复
热议问题