C++, Integer and Long integer Literals

Adding L to the end of a Literal

Page 1 of 1

5 Replies - 7783 Views - Last Post: 23 January 2008 - 06:47 AM Rate Topic: -----

#1 Pourang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-September 07

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?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: C++, Integer and Long integer Literals

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++, Integer and Long integer Literals

Posted 20 January 2008 - 01:31 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 Pourang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-September 07

Re: C++, Integer and Long integer Literals

Posted 20 January 2008 - 02:01 PM

View PostNickDMax, on 20 Jan, 2008 - 01:31 PM, said:

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... .

But this is the same, useless. or i'm wrong?

:-)
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.

//unsigned short int Value = 65536; //Error! 65536 is too big
unsigned long int Value = 65536L;


Was This Post Helpful? 0
  • +
  • -

#5 Pourang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-September 07

Re: C++, Integer and Long integer Literals

Posted 21 January 2008 - 03:28 AM

View PostNickDMax, 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.

//unsigned short int Value = 65536; //Error! 65536 is too big
unsigned long int Value = 65536L;


Thanks
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: C++, Integer and Long integer Literals

Posted 23 January 2008 - 06:47 AM

This also depends on the type of computer (and even compiler) you are using.
The size of an integer is 2 bytes on a 16-bit machine, 4 bytes for 32-bit and 8 bytes for 64-bit systems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1