0

I have been preparing for interview when I bumped up with this question.

#include<stdio.h>
int main()
{

    unsigned long a = 100;
    long b = -1;
    if(b>a)
        printf("YES");
    else
        printf("No");
}

The output of the program is to be found. The answer is YES can anyone please explain me how this is the correct answer? I analyzed and found that the answer is YES when atleast one of a and b has unsigned qualifier. When both are just long then it prints NO

EDIT:

There is one other question which made me think much. Here is the code

#include<stdio.h>
int main()
{

    float t = 1.0/3.0;
    if(t*3 == 1.0)
        printf("yes");
    else
        printf("no");

}

The answer for the code is no but I am not able to decipher how it is obtained. Also, when I assume a variable a = t*3 and compare it in the if statement, I get the output as yes

I am trying to learn the concepts. So Kindly help me by explaining how these 2 programs produce the respective output.

Thanks

vivek1794
  • 111
  • 2
  • 13
  • When they're both `long`, -1 > 100? – FatalError Mar 11 '14 at 17:36
  • @FatalError when they are both just `long` then the ans is NO that is it says -1<100 which we all knew. I want to know why it is the reverse when I use unsigned? – vivek1794 Mar 11 '14 at 17:38
  • 4
    Look up integer conversion rules. Or https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules . Or http://stackoverflow.com/questions/19273658/integer-conversionsnarrowing-widening-undefined-behaviour – zubergu Mar 11 '14 at 17:39
  • @vivek1794 Same size `signed long` and `unsigned long` are compared as `unsigned long`. The conversion of `b` to an `unsigned long` results in a value great than 100. – chux - Reinstate Monica Mar 11 '14 at 17:48
  • possible duplicate of [Why is this happening with the sizeof operator?](http://stackoverflow.com/questions/22047158/why-is-this-happening-with-the-sizeof-operator) – Shafik Yaghmour Mar 11 '14 at 17:50
  • If you have a compiler where `(long) -1 > (long) 100`, you have a *very broken* compiler... – twalberg Mar 11 '14 at 17:54

3 Answers3

1

Because of C usual arithmetic conversions. Both operands of > operator are converted to a single common type. When

unsigned long a = 100;
long b = -1;

The if controlling expression:

b > a

is equivalent to

(unsigned long) b > a

which is equivalent here as:

ULONG_MAX > 100UL

because converting -1 to unsigned long evaluates to ULONG_MAX.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

When you do a comparison, the compiler has to convert both operands to the same type. The conversion is based on "rank" (size) and signedness.

If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type.

You compare an unsigned long with a long. According to the rule above, the long b is converted to unsigned long, resulting in the greatest possible positive number. Note, the actual bits do not change. 0xFFFFFFFF as a 32-bit signed means -1. As a 32-bit unsigned it means 4294967295.

As for the floats, the == operator compares the floats bit-by-bit. But you've lost some precision during the division and multiplication.

SzG
  • 12,333
  • 4
  • 28
  • 41
0

From usual arithmetic conversion:

"If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type."

before:

unsigned long -- signed long

100=...0001100100 -1 = ...11111111111

after:

unsigned long -- signed long converted to unsigned long

100 -- 2^n - 1

n- number of bits for long.

-1>100 ? No, 2^n -1 > 100.

zubergu
  • 3,646
  • 3
  • 25
  • 38