Adding signed and unsigned int
Asked Answered
I

4

13
#include <stdio.h>

int main(void)
{
    unsigned int a = 6;
    int b = -20;

    a + b > 6 ? puts("> 6") : puts("<= 6");
}

It is clear to me how the ternary operator work in this code. I am not able to understand the addition of the signed and unsigned integer here.

When running this code, the output is > 6; why?

Ideal answered 18/10, 2013 at 10:2 Comment(10)
learn about ternary operator : en.wikipedia.org/wiki/%3F:#CAres
geeksforgeeks.org/archives/9205Androgen
@georgesl I think the whole "challenge" is about knowing of what type is the addition a + b between unsigned int a and int b. (Which frankly, I couldn't care less about, because my coding standards forbid performing arithmetic operations without previously converting everything to an explicit same type.)Lovely
@DanielDaranas: Quite possibly. But the OP would do well to make this clear in the question ;)Marinna
It's hard to give a concise answer to the question without knowing exactly what part of the program you don't understand.Rensselaerite
duplicate: #1597905Mintz
I am not confused about the ternary operator but about the addition of a signed and unsigned integerIdeal
@DanielDaranas : oh I missed that ! that's really evil ...Ares
MISRA C probably shouts at you in reversed Latin if you throw this kind of expression to it (arithmetic involving implicit conversions between signed and unsigned types).Lovely
See this: https://mcmap.net/q/174283/-is-unsigned-integer-subtraction-defined-behaviorCosecant
L
30

I think the OP is not confused about the ternary operator its a different problem.

From the C99 standard, section 6.3.1.8 ("Usual arithmetic conversions"):

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.

unsigned int and int has the same rank, so it's equivalent to:

(a + (unsigned int)b > 6)

To fix it, you need to explicitly cast in the other direction, i.e.:

((int)a + b > 6)

So this is the reason the output will be >6 and NOT <=6

Loehr answered 18/10, 2013 at 10:9 Comment(0)
F
9

The other two answer accurately describe Ternary operator, but I think this is more relevant to the question

The output is >6 because (a + b) casts b to unsigned int as well.

EDIT:

See Acme's suggestion for fixing this problem. Essentially casting a as an int will fix this

Fresco answered 18/10, 2013 at 10:9 Comment(0)
G
5

the simple form of you code is as follow:

if(a + (unsigned int)b > 6)
{
    puts(">6")
}
else
{
    puts("<=6");
}

Output will be :

>6 as (a + (unsigned int)b > 6)
Gowan answered 18/10, 2013 at 10:9 Comment(0)
R
5

Because 4294967282>6 is true you will get >6 as output. 4294967282 is coming from assigning -14 to a unsigned int. (a+b) will be converted as 2^32 - 14. `

Rastus answered 18/10, 2013 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.