sun qingyao sun qingyao - 1 month ago 5
C Question

What is sizeof(2.5) equal to?

An examination question was:


Q2:
sizeof(2.5)
is equal to ____.

A.1

B.2

C.3

D.4


There are no such suffixes as
f l F L
, so floating-point constant
(2.5)
has a double type. Since nothing else is specified, I suppose its value is unspecified. For example, on some implementations whose
CHAR_BIT
is 32(causing
sizeof (char)==sizeof (long)
, maybe), the answer can be B.

However, in most cases, where
CHAR_BIT
is 8, the answer should be 8, but 8 isn't listed on the paper!

When asked such a ridiculous question in a exam/interview, what should I answer?

PS: Just ignore these useless parentheses please :)
PPS: Is IEEE 754 granted by the standard?

Answer

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.

Comments