您的位置:程序门 -> vc/mfc -> 基础类



一个关于int 变量的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


一个关于int 变量的问题[已结贴,结贴人:itis_ohmygod]
发表于:2007-06-26 18:27:06 楼主
#include       <iostream.h>      
    void       main()      
    {      
    int       a=-20;      
    int       c=-30;  
    unsigned       int       b=10;  
    cout < < "a=       " < <a < <endl;    
    cout < < "b=       " < <b < <endl;  
    cout < < "c=       " < <c < <endl;  
    cout < < "a+c=       " < <a+c < <endl;      
    cout < < "a+b=       " < <a+b < <endl;  
    }      
我想问一下   a+b=   4294967286为什么呢?
发表于:2007-06-26 18:46:351楼 得分:10
实际上是溢出了,因为   unsigned       int比int优先级高,两个在相加的时候结果转成了   unsigned       int,而你的结果是负数,表示不了
发表于:2007-06-26 18:51:482楼 得分:5
ls说得对,你a+b的结果为负   转化为unsigned溢出,转化为整形才行int   d   =a+b;
发表于:2007-06-26 18:58:393楼 得分:0
我在做一个生产者和消费者的多线程小东西
用互斥对象
我用全局的变量int   iconsumer;
可是在消费者线程函数里
iconsumer   =   0;
iconsumer   =   iconsumer   -10;
::setdlgitemint(hwnd,idc_static_consumer,iconsumer,false);
输出的数据却是4994967686这样的结果  
我不理解   怎么会这样
麻烦帮我看看
发表于:2007-06-26 18:59:224楼 得分:5
相当于你用unsigned   int   去描述了一个负数,其最终表示为4294967286,正如你所看到的..
发表于:2007-06-26 19:01:005楼 得分:0
0算有符号还是无符号
补充问一下
发表于:2007-06-26 19:01:596楼 得分:30
32位系统int占4个字节,计算机中数是以补码形式参加运算的
-20   =   11111111111111111111111111101100
10   =   1010
-20   +   10   无符号运算为
11111111111111111111111111101100   +   1010   =   11111111111111111111111111110110
也就是4294967286
发表于:2007-06-26 19:05:007楼 得分:0
bool   setdlgitemint(
    hwnd   hdlg,               //   handle   of   dialog   box
    int   niddlgitem,     //   identifier   of   control
    uint   uvalue,           //   value   to   set
    bool   bsigned           //   signed   or   unsigned   indicator
);
第三个参数是   unsigned   int   也就是系统把的负数当无符号处理了,所以出现你所说的问题
发表于:2007-06-26 19:05:298楼 得分:0
可是我没有定义unsigned   int无符号类型啊?
发表于:2007-06-26 19:06:039楼 得分:0
对哦   谢谢   我太不仔细了   呵呵
发表于:2007-06-26 19:06:4310楼 得分:0
是的啊,你看一下   setdlgitemint第三个参数是uint   也就是unsigned   int
发表于:2007-06-26 19:09:5011楼 得分:0
可是,这个问题我该怎么解决呢?
发表于:2007-06-26 19:12:4112楼 得分:0
建义你用wsprintf把你的数值写入个字符串中,然后用setdlgitemtext就可以了
发表于:2007-06-26 19:14:5613楼 得分:0
恩   好的   谢谢   我试一试   :)
发表于:2007-06-26 19:15:3114楼 得分:0
bsigned  
specifies   whether   the   uvalue   parameter   is   signed   or   unsigned.   if   this   parameter   is   true,   uvalue   is   signed.   if   this   parameter   is   true   and   uvalue   is   less   than   zero,   a   minus   sign   is   placed   before   the   first   digit   in   the   string.   if   this   parameter   is   false,   uvalue   is   unsigned.  
还有看看这样行不行::setdlgitemint(hwnd,idc_static_consumer,iconsumer,true);
我没试,你自己看不行就用我上面说的方法一定行


快速检索

最新资讯
热门点击