zoukankan      html  css  js  c++  java
  • 整型字面常量(Integer Literals)的类型匹配

    有下面两行代码:

    for(int i=0; i<0xFFFFFFFF; ++i);
    
    for(char ch=0; ch<0xFF; ++ch);

    第一行代码不会陷入死循环,第二行代码则会死循环。

    如果不明白,看了下面的内容,你应该就了解了:

     

    整型常量的类型

    The data type of an integer literal is determined by its form, value, and suffix.

    The following table lists the integer literals and shows the possible data types. The smallest data type that can represent the constant value is used to store the constant.

    Integer Literal

    Possible Data Types

    unsuffixed decimal

    int, long int, unsigned long int, long long int

    unsuffixed octal

    int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

    unsuffixed hexadecimal

    int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

    decimal, octal, or hexadecimal suffixed by u or U

    unsigned int, unsigned long int, unsigned long long int

    decimal suffixed by l or L

    long int, long long int

    octal or hexadecimal suffixed by l or L

    long int, unsigned long int, long long int, unsigned long long int

    decimal, octal, or hexadecimal suffixed by both u or U, and l or L

    unsigned long intunsigned long long int    

    decimal suffixed by ll or LL

    long long int

    octal or hexadecimal suffixed by ll or LL

    long long int, unsigned long long int

    decimal, octal, or hexadecimal suffixed by both u or U, and ll or LL

    unsigned long long int

     

    参考:http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc02ic.htm

    16 进制Integer literal 的类型是下面第一个能表示其值的类型:
    int, long int, unsigned long int, long long int
    10 进制Integer literal的类型是下面第一个能表示其值的类型:
    int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

    整型变量或常量比较时的类型转换

    int i = -1;
    unsigned int j = 1;
    if(i > j)
    {
        //do something
    }

    上面代码中的i和j不是相同的类型,比较时会把非unsigned的i的类型转换为unsigned int.导致的结果就是something will be done.

     

    原来第一个for循环中的0xFFFFFFFF 被识别成了unsigned int,比较时i被转换成unsigned int 来进行比较

    而第二个for循环中的0xFF则在int的表示范围之内所以被识别成了int类型

     

    以上只在VC6中测试通过,尽管鄙视吧

  • 相关阅读:
    04.
    24
    39
    46
    72.
    21.
    logout: not found”
    Username is not in the sudoers file. This incident will be reported
    激活函数
    排序算法
  • 原文地址:https://www.cnblogs.com/xueguangfeng/p/Integer_Literals_Type.html
Copyright © 2011-2022 走看看