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.
Related videos on Youtube
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, 2022Comments
-
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 of0LL
in above code?When should I prefer
0LL
over0
?-
kay about 9 years
long long
is asigned
type. It makes no difference if you writex > 0ll
orx > 0
, because0
is automatically promoted to along long
.
-
-
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 about 9 yearsDidn'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 about 9 yearsSo when we should use
0LL
instead of0
? -
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);
), andunsigned n; int i; ... long long l = 0LL + n + i;
(iflong long
covers the range ofunsigned int
, what it probably always does, this prevents promotion ofi
tounsigned
).