8
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?

Deqing
  • 14,098
  • 15
  • 84
  • 131
  • 1
    `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`. – Kijewski Feb 17 '15 at 00:26
  • http://stackoverflow.com/questions/13275136/which-initializer-is-appropriate-for-an-int64-t – Alex Reynolds Feb 17 '15 at 00:27
  • Actually, the "signedness" of `long long` doesn't matter for this specific case. If the comparison was `llIdx >= 0` (or `0LL`) it would, because that predicate would always be true for `unsigned long long`. – kdopen Feb 17 '15 at 00:38

1 Answers1

12

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.

kdopen
  • 8,032
  • 7
  • 44
  • 52
  • "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. – Kijewski Feb 17 '15 at 00:28
  • 1
    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" – kdopen Feb 17 '15 at 00:31
  • So when we should use `0LL` instead of `0`? – Deqing Feb 17 '15 at 03:52
  • @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`). – mafso Feb 17 '15 at 10:08