0
int main()
{
    unsigned long a= 100000;
    long b = -1;
    if (b > a)
        printf("yes\n");
    else
        printf("No\n");

    return 0;
 }

Why the output comes "Yes" when we know 100000 > -1 . So accordingly it should print "NO" but being a naive, i can really get it

Rob
  • 14,746
  • 28
  • 47
  • 65

2 Answers2

5

When you compare long and unsigned long then both get converted to unsigned long first so (depending on the platform) the -1 value becomes 0xFFFFFFFF. The result is clear then.

dlask
  • 8,776
  • 1
  • 26
  • 30
2

In this line:

if (b > a)

you are comparing an unsigned long with a long, which leads to the errors.

Simply change

 unsigned long a= 100000;

to

  long a= 100000;
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
  • Okay i get what u meant , but i was wondering how **unsigned** made the value of **long a** less than **long b** ? – Shijith Thomas Jun 21 '15 at 12:59
  • @SJith: See, unsigned means its values will be from 0, and no negatives can be included. So when you try to fit a negative number into an unsigned, implicitly it is converted into a non-negative. So, -1 will be converted to, say 2^32-1, which is larger than 10000, so you get your output. – Ayushi Jha Jun 21 '15 at 13:08
  • Thanks @karma_geek :) – Shijith Thomas Jun 26 '15 at 14:40