Return value of sizeof() in c?
This question already has an answer here:
Why does this program gives false as output?
if(sizeof(int) > -1)
The reason is that sizeof
returns size_t
(unsigned), so -1
was converted to unsigned before comparing.
According to the standard:
6.3.1.8 Usual arithmetic conversions
....
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
Note that if the second operand has a greater rank, the result is different. My compiler gives true
for long long
:
if (sizeof(int) > -1LL)
sizeof
returns size_t
( unsigned
type) . You are comparing a signed int
with an unsigned int
. When a signed
operand is compared with unsigned
one, the signed
operand get converted to an unsigned
value.
keyword sizeof followed by ellipsis returns the number of elements in a parameter pack. The type of the result is the unsigned
integral type size_t
defined in the header file. So you are comparing an unsigned int with a signed int.
上一篇: 奇怪的TRUE和FALSE宏的定义
下一篇: sizeof()在c中的返回值?