为什么 unsigned int 包含负数 [英] Why unsigned int contained negative number

查看:80
本文介绍了为什么 unsigned int 包含负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对无符号数字(unsigned short、int 和 longs)的了解,它仅包含正数,但以下简单程序成功地将负数分配给了无符号整数:

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 negative number to an unsigned int:

  1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  prog4.c
  5  *
  6  * =====================================================================================
  7  */
  8 
  9 #include <stdio.h>
 10 
 11 int main(void){
 12 
 13     int v1 =0, v2=0;
 14     unsigned int sum;
 15     
 16     v1 = 10;
 17     v2 = 20;
 18     
 19     sum = v1 - v2;
 20     
 21     printf("The subtraction of %i from %i is %i \n" , v1, v2, sum);
 22     
 23     return 0;
 24 }

输出是:20减10是-10

推荐答案

%i有符号整数的格式说明符;你需要使用 %u 来打印一个无符号整数.

%i is the format specifier for a signed integer; you need to use %u to print an unsigned integer.

这篇关于为什么 unsigned int 包含负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆