In the expression of the if statement
if (SYS_FS_FileWrite(handle, buf, nbytes) == -1) {
there are used two operands of different types. The left operand of the equality operator has the type size_t
and the right operand is the integer constant -1
of the type int
.
The compiler needs to determine the common type of the operands.
In this case there are used the so-called usual arithmetic conversions.
As the rank of the type size_t
is not less than the rank of the type int
(the typs size_t
is usually an alias for the type unsigned long
and in more rare cases for type unsigned long long
) then the operand of the type int
is converted to the type size_t
.
So under the hood you have in fact
if (SYS_FS_FileWrite(handle, buf, nbytes) == ( size_t )-1) {
the same way as the returned integer constant -1
from the function is converted to the type size_t
according to the function return type
size_t SYS_FS_FileWrite(SYS_FS_HANDLE handle, const void *buf, size_t nbytes)
{
//...
return ( size_t ) -1;
//...
}
Thus the if statement may be interpreted like
if ( ( size_t )-1 == ( size_t )-1) {
provided that the function returns -1
. Otherwise the left operand of the equation has some other value returned by the function and the expression evaluates to logical false (integer value 0
)
From the C Standard (6.5.9 Equality operators)
4 If both of the operands have arithmetic type, the usual arithmetic
conversions are performed. Values of complex types are equal if and
only if both their real parts are equal and also their imaginary parts
are equal. Any two values of arithmetic types from different type
domains are equal if and only if the results of their conversions to
the (complex) result type determined by the usual arithmetic
conversions are equal.