Adding L to the end of a Literal

C++, Integer and Long integer Literals

Posted 20 January 2008 - 01:11 PM

Hi.

Adding a L to the end of a literal like 32 couse : The literal use 4 bytes. ( like a Long integer ).

But i see that no matter there is a L or not, whenever i use the sizeof(), this is the variable defination which decides how many bytes this variable can save and there is no information indicating that number 32 using more bytes than usual.

short int number = 3L;
cout << sizeof(number);

i can't figure it out. 2 bytes still. should'nt this make the number to save 32 as an long integer?

Re: C++, Integer and Long integer Literals

The data type of the variable 'number' is still 'short int' so the compiler will convert the 'long int' value (32L) into the 'short int'.

if you want it to be a long you need to put the value into a variable with a long int data type.

The thing is if i do so, so in which cases i need to add a L anyway? From " Starting out with c++" : If you are in a situation where you have an integer literal, nut you need it to be stored in memory as an long integer... .

Re: C++, Integer and Long integer Literals

Posted 20 January 2008 - 03:07 PM

Generally speaking you don't need to add the "L" at the end of an integer. The problem is if you have a BIG number that is outside of the bounds of an int. So 65536 will not fit into a signed short int (2 bytes) so the compiler will want to see this as 65536L.

Re: C++, Integer and Long integer Literals

Posted 21 January 2008 - 03:28 AM

NickDMax, on 20 Jan, 2008 - 03:07 PM, said:

Generally speaking you don't need to add the "L" at the end of an integer. The problem is if you have a BIG number that is outside of the bounds of an int. So 65536 will not fit into a signed short int (2 bytes) so the compiler will want to see this as 65536L.