First of all, the value created by sizeof is size_t , type unsigned . Note
Since the unsigned type has a higher rank than the signed type, when performing the comparison, in accordance with the norms of the relation operator, the usual arithmetic conversions are performed, which means that the signed type is advanced to the unsigned type.
In your case, -1 , if it is considered unsigned , is the maximum possible unsigned value, so itβs not surprising
if (sizeof(int) > -1)
Returns false.
Moral of the story: An attempt to compare between signed and unsigned is expected to produce a strange result, as in your case. You must enable the compiler warning and try to solve the problems that the compiler reports.
Note:
From C11 , chapter Β§7.19, <stddef.h> ,
size_t
which is an unsigned integer type of the result of the sizeof operator.
source share