Can I assume the size of long int is always 4 bytes?

前端 未结 10 818
闹比i
闹比i 2020-12-07 18:26

Is it always true that long int (which as far as I understand is a synonym for long) is 4 bytes?

Can I rely on that? If no

10条回答
  •  离开以前
    2020-12-07 19:02

    The standard says nothing about the size of long int, so it is dependent on the environment which you are using.

    To get the size of long int on your environment you can use the sizeof operator and get the size of long int. Something like

    sizeof(long int)
    

    C standard only requires the following points about the sizes of types

    • int >= 16 bits,
    • long >= 32 bits,
    • long long (since C99) >= 64 bits
    • sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
    • sizeof(char) == 1
    • CHAR_BIT >= 8

    The remaining are implementations defined, so it's not surprised if one encountered some systems where int has 18/24/36/60 bits, one's complement signed form, sizeof(char) == sizeof(short) == sizeof(int) == sizeof(long) == 4, 48-bit long or 9-bit char like Exotic architectures the standards committees care about and List of platforms supported by the C standard

    The point about long int above is completely wrong. Most Linux/Unix implementations define long as a 64-bit type but it's only 32 bits in Windows because they use different data models (have a look at the table here 64-bit computing), and this is regardless of 32 or 64-bit OS version.

    Source

提交回复
热议问题