Comparing long long with 0

17,301

Yes, you can use a plain 0 here. The compiler would look at the type of each argument to > and promote the smaller one so that they are the same size.

Thus llIdx > 0 and llIdx > 0LL are equivalent.

Share:
17,301

Related videos on Youtube

Deqing
Author by

Deqing

9+ years software development working experience with C++ and C under Linux/UNIX and Windows. Knowledge in Telecom Networks and Open Source Technologies.

Updated on June 04, 2022

Comments

  • Deqing
    Deqing almost 2 years
    long long llIdx = foo();
    if (llIdx > 0LL) // Can I use 0 here?
      ...
    

    Is there any problem if I use 0 instead of 0LL in above code?

    When should I prefer 0LL over 0?

    • kay
      kay about 9 years
      long long is a signed type. It makes no difference if you write x > 0ll or x > 0, because 0 is automatically promoted to a long long.
  • kay
    kay about 9 years
    "The compiler would look at the type of each argument to > and promote the smaller one so that they are the same size." That's why I didn't put my comment as an answer. There is a bunch of special cases if the sign of both integers vary.
  • kdopen
    kdopen about 9 years
    Didn't see your comment while I was typing :) I was just trying to answer the specific question with a little more than "yes". And I presume you meant "signedness", not "sign"
  • Deqing
    Deqing about 9 years
    So when we should use 0LL instead of 0?
  • mafso
    mafso about 9 years
    @Deqing: I can't come up with a good example where usual arithmetic conversion does something unexpected if one operand has value 0. Variadic arguments are one (e.g. printf("%lld", 0LL);), and unsigned n; int i; ... long long l = 0LL + n + i; (if long long covers the range of unsigned int, what it probably always does, this prevents promotion of i to unsigned).