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?
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 afloat
, and also erroneously assumesfloat
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 definitely4
, and it would make sense to try and fool the student with theC
answer.the question might be misspelt, such as
sizeof('2.5')
but missing the'
. For this very ugly question, the answer is the same assizeof(int)
, often4
on systems with 8 bit bytes. Note thatsizeof(2,5)
is also the same assizeof(int)
, but that would be a trick question too.on some DSP systems where
double
is 64 bits andchar
16 bits, answer4
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
).
来源:https://stackoverflow.com/questions/34803572/what-is-sizeof2-5-equal-to