C ordinary arithmetic conversions - the latter case in the standard

Trying to parse the standard, the latter case seems to be where we have a signed and unsigned operand where the signed operand is of higher rank but still cannot match the entire range of the unsigned operand below the rank. It looks like we have signed long and unsigned int in 32 bit architecture. The standard says the following:

Otherwise, both operands are converted to an unsigned integer type corresponding to the type of the signed integer operand.

If I read this correctly then

long a = -3;
unsigned int b = 2;
a + b;

      

It leads to the fact that a

, and b

the two will be converted to an unsigned long before adding? It's right? I don't have a 32-bit machine to test, but does that mean what a+b

should matter ULONG_MAX

? Could this be correct?

+3


source to share


1 answer


That means you think both operands are promoted to enter unsigned long

according to the rule given, see live example (ideon is apparently 32-bit):

#include <stdio.h>
#include <limits.h>

int main(void)
{
    printf("%zu\n", sizeof(int));
    printf("%zu\n", sizeof(long));

    long a = -3;
    unsigned int b = 2;

    printf("%lu\n", a + b);
    printf("%lu\n", -1UL);
    printf("%lu\n", ULONG_MAX);

    return 0;
}

      



Result:

4
4
4294967295
4294967295
4294967295

      

+3


source







All Articles