Is the size of C “int” 2 bytes or 4 bytes?

后端 未结 13 2723
不知归路
不知归路 2020-11-22 10:52

Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on?

Most of the textbooks say integer variables occupy 2 bytes. But whe

13条回答
  •  眼角桃花
    2020-11-22 11:16

    There's no specific answer. It depends on the platform. It is implementation-defined. It can be 2, 4 or something else.

    The idea behind int was that it was supposed to match the natural "word" size on the given platform: 16 bit on 16-bit platforms, 32 bit on 32-bit platforms, 64 bit on 64-bit platforms, you get the idea. However, for backward compatibility purposes some compilers prefer to stick to 32-bit int even on 64-bit platforms.

    The time of 2-byte int is long gone though (16-bit platforms?) unless you are using some embedded platform with 16-bit word size. Your textbooks are probably very old.

提交回复
热议问题