why sizeof(13.33) is 8 bytes?

后端 未结 5 663
醉酒成梦
醉酒成梦 2020-12-06 18:38

When I give sizeof(a), where a=13.33, a float variable, the size is 4 bytes. But if i give sizeof(13.33) directly, the size is 8 bytes

5条回答
  •  悲哀的现实
    2020-12-06 19:17

    The type and size of your variable are fine. It's just that the compiler has some default types for literals, those constant values hard-coded in your program.

    If you request sizeof(1), you'll get sizeof(int). If you request sizeof(2.5), you'll get sizeof(double). Those would clearly fit into a char and a float respectively, but the compiler has default types for your literals and will treat them as such until assignment.

    You can override this default behaviour, though. For example:

    2.5 // as you didn't specify anything, the compiler will take it for a double.
    2.5f // ah ha! you're specifying this literal to be float
    

    Cheers!

提交回复
热议问题