What is sizeof(2.5) equal to? [closed]

守給你的承諾、 提交于 2019-12-08 17:47:29

This question is inconsistent: sizeof(2.5) is implementation defined. Without extra context, one cannot determine which answer to choose.

If this is an oral examination, you can explain this to the examiner and should get a very good mark given how much you already know. (btw IEEE-754 is not mandated by the Standard, but a very common implementation of double).

If it is a written exam for which you cannot give further explanation (the marginal space being too exiguous), you should select 4 for the following reasons:

  • the examiner might erroneously assume 2.5 to be a float, and also erroneously assumes float to always be 32 bits, further erroneously assuming bytes to be 8 bits. This is the most probable explanation. Be careful in a oral exam to stay humble if you think you know better than the examiner, it is probably the case here.

  • the question might be misspelt, such as sizeof("2.5") but missing the ". For this question, the answer is definitely 4, and it would make sense to try and fool the student with the C answer.

  • the question might be misspelt, such as sizeof('2.5') but missing the '. For this very ugly question, the answer is the same as sizeof(int), often 4 on systems with 8 bit bytes. Note that sizeof(2,5) is also the same as sizeof(int), but that would be a trick question too.

  • on some DSP systems where double is 64 bits and char 16 bits, answer 4 would be correct.

  • other answers are even less likely to be correct, except of course on the DS9K computer.

sizeof(2.5) will yield the size of a double in bytes-- which is implementation defined.

From the standard:

5.3.3 Sizeof
The sizeof operator yields the number of bytes in the object representation of its operand.
The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined.

sizeof returns the size in chars, not bits. So, for example, sizeof(char) is by definition 1.

However, this question is not very well written since it is system specific. My guess is that the sizeof of a double on your system is 8, which would be 64-bit since (8 * CHAR_BIT) == `` (assuming the common value of CHAR_BIT as 8).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!