V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
jyoe
V2EX  ›  iDev

请教Xcode里的int类型所占字节长度和linux不一样吗?

  •  
  •   jyoe ·
    woody1983 · 2012-07-26 21:57:52 +08:00 · 5869 次点击
    这是一个创建于 4285 天前的主题,其中的信息可能已经有所发展或是发生改变。
    unsigned int int_x = 4294967295;
    printf("%d \n",int_x); // 1111-1111-1111-1111-1111-1111-1111-1111

    unsigned int int_y = 4294967296;
    printf("%d \n",int_y);// 1-00000000000000000000000000000000

    unsigned int int_z = 4294967297;
    printf("%d \n",int_z);//1-00000000000000000000000000000001

    unsigned情况下 int类型的话4个byte位全部用来做存储 可为什么输出的时候是
    -1
    0
    1

    4294967296和4294967297的输出是正常的 为什么第一会输出-1 求教!
    8 条回复    1970-01-01 08:00:00 +08:00
    nivlac
        1
    nivlac  
       2012-07-26 22:57:57 +08:00
    一样的,最高位是符号位,负数是补码
    xuan_lengyue
        2
    xuan_lengyue  
       2012-07-26 23:00:47 +08:00
    int的长度和编译器、机器字长都有关系。
    C标准没有限定,只规定了最短四个字节。
    godwarlock
        3
    godwarlock  
       2012-07-26 23:53:12 +08:00   ❤️ 1
    %d 是用来显示 int的
    %ud 是用来显示 unsigned int 的

    int长度如 @xuan_lengyue 所说并没有一个规范
    leafduo
        4
    leafduo  
       2012-07-27 00:43:48 +08:00
    @xuan_lengyue 最短是2个字节吧我印象中?
    leafduo
        5
    leafduo  
       2012-07-27 00:52:25 +08:00   ❤️ 1
    @xuan_lengyue basic signed integer type. At least 16 bits in size.
    xuan_lengyue
        6
    xuan_lengyue  
       2012-07-27 00:58:44 +08:00
    @leafduo 哈哈,记错了,多谢 ==
    jyoe
        7
    jyoe  
    OP
       2012-07-27 10:37:05 +08:00
    @leafduo short类型是2个字节 就是16个bit位
    jyoe
        8
    jyoe  
    OP
       2012-07-27 10:38:26 +08:00
    @godwarlock 感谢感谢~漏了一个%ud
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1404 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:23 · PVG 01:23 · LAX 10:23 · JFK 13:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.