C interpretation of hexadecimal long integer literal “L”

后端 未结 2 1851
-上瘾入骨i
-上瘾入骨i 2021-01-25 16:06

How does a C compiler interpret the \"L\" which denotes a long integer literal, in light of automatic conversion? The following code, when run on a 32-bit platform (32-bit long

相关标签:
2条回答
  • 2021-01-25 16:50

    It's a hexadecimal literal, so its type can be unsigned. It fits in unsigned long, so that's the type it gets. See section 6.4.4.1 of the standard:

    The type of an integer constant is the first of the corresponding list in which its value can be represented.

    where the list for hexadecimal literals with a suffix L is

    1. long
    2. unsigned long
    3. long long
    4. unsigned long long

    Since it doesn't fit in a 32-bit signed long, but an unsigned 32-bit unsigned long, that's what it becomes.

    0 讨论(0)
  • 2021-01-25 17:03

    The thing is that the rules of determining the type of the integral literal are different depending on whether you have a decimal number or a hexadecimal(or octal number). A decimal literal is always signed unless postfixes with U. A hexadecimal or octal literal can also be unsigned if the signed type can not contain the value.

    0 讨论(0)
提交回复
热议问题