Using the letter L in long variable declaration

PSR picture PSR · Jul 19, 2013 · Viewed 7.8k times · Source
long l2 = 32;

When I use the above statement, I don't get an error (I did not used l at the end), but when I use the below statement, I get this error:

The literal 3244444444 of type int is out of range

long l2 = 3244444444;

If I use long l2 = 3244444444l;, then there's no error.

What is the reason for this? Using l is not mandatory for long variables.

Answer

Luiggi Mendoza picture Luiggi Mendoza · Jul 19, 2013

3244444444 is interpreted as a literal integer but can't fit in a 32-bit int variable. It needs to be a literal long value, so it needs an l or L at the end:

long l2 = 3244444444l; // or 3244444444L

More info: