Should I always use LL when assigning a value to long long in C++?
Assuming I have
long long myVar = 10LL;
Or If I have a member variable of long long type should I always use LL?
A::A()
: m_myVar(0LL)
{
}
What are possible dangers to not write LL or to write L?
EDITED From answers, comments and further googling the dangerous is such a case
m_myVar = 1000000000 * 100;
if LL will be forgotten for 1000000000 or 100 the overflow will happen.
No you don't need to do that. A C++ compiler will make the conversion.
(For the avoidance of doubt, long long a = 5000000000; is well-defined on a platform that has 32 bit int and long.)
Sometimes though, particularly when you work with templates, you will need to let the compiler know the type of literal you need.
For example, if a is an unsigned then std::max(a, 1) will fail since 1 is a signed type: you'd need to write 1U or something similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With