Of int, char, float, and bool, which is smallest?

后端 未结 10 1288
傲寒
傲寒 2021-02-02 09:51

The following is from a \"fill-in at home\" programming test that is part of the application process for an MSc in game development at a UK university:

C+

10条回答
  •  孤城傲影
    2021-02-02 10:17

    There is no guarantee for the exact size of these types, but there is a guarantee, that char is not bigger than short, and short is not bigger than long.

    So, char will always occupy the least amount of memory, but it might not be the only one to do so. It's still guaranteed, that nothing else will have a smaller size.

    There might be an exception with bool, however, on some special embedded microcontrollers. They can have a bit variable, which takes exactly one bit, however, they are not in RAM but in special registers.

    However, unless your architecture and compiler are especially strange or unusual, you can reasonalbly expect that char is 1, short is 2, long is 4, long long is 8 and int is either 2 or 4, but usually 4 bytes long.

提交回复
热议问题