C interpretation of hexadecimal long integer literal “L”

后端 未结 2 1852
-上瘾入骨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.

提交回复
热议问题