Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use LL when assigning a value to long long c++

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.

like image 407
T M Avatar asked Dec 10 '22 18:12

T M


1 Answers

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.

like image 117
Bathsheba Avatar answered Dec 31 '22 15:12

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!