Why nData
cannot be 12.0
or 12
by sizeof(nData += 2.0)
?
Well, sizeof
is a compiler time operator, and it operates on the data type, not the value.
In other words, except when the argument is VLA, the operand of the sizeof
is not evaluated.
Quoting C11
, chapter §6.5.3.4
The sizeof
operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of
the operand. If the type of the operand is a variable length array
type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
integer constant.
So, in your case,
printf("%d", sizeof(nData + 2.0)); // data == int, 2.0 == double
is the same as
printf("%d", sizeof(double)); // as the resulting "type" of the addition,
// 'int + double' would be 'double'
which should better be
printf("%zu", sizeof(double)); //not %d
as sizeof
yields size_t
type.
Also, regarding the 2.0
being of type double
, from chapter §6.4.4.2
A floating constant has a significand part that may be followed by an exponent part and a
suffix that specifies its type. The components of the significand part may include a digit
sequence representing the whole-number part, followed by a period (.), followed by a
digit sequence representing the fraction part. [...]
and
An unsuffixed floating constant has type double
. [...]
Thus, a constant value like 2.0
has the type double
.