10
#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?

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Nargis
  • 4,687
  • 1
  • 28
  • 45
  • learn about ternary operator : http://en.wikipedia.org/wiki/%3F:#C – lucasg Oct 18 '13 at 10:06
  • http://www.geeksforgeeks.org/archives/9205 – opalenzuela Oct 18 '13 at 10:07
  • 3
    @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.) – Daniel Daranas Oct 18 '13 at 10:07
  • @DanielDaranas: Quite possibly. But the OP would do well to make this clear in the question ;) – Oliver Charlesworth Oct 18 '13 at 10:08
  • It's hard to give a concise answer to the question without knowing exactly what part of the program you don't understand. – Dan Hulme Oct 18 '13 at 10:08
  • duplicate: http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value – Maquefel Oct 18 '13 at 10:08
  • 3
    I am not confused about the ternary operator but about the addition of a signed and unsigned integer – Nargis Oct 18 '13 at 10:08
  • @DanielDaranas : oh I missed that ! that's really evil ... – lucasg Oct 18 '13 at 10:11
  • [MISRA C](http://en.wikipedia.org/wiki/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). – Daniel Daranas Oct 18 '13 at 10:13
  • See this: http://stackoverflow.com/a/15030332/1168156 – LihO Oct 18 '13 at 10:13

4 Answers4

26

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

Sadique
  • 22,572
  • 7
  • 65
  • 91
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

sukhvir
  • 5,265
  • 6
  • 41
  • 43
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)
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
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. `

Dayal rai
  • 6,548
  • 22
  • 29