Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,454 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,136 people online right now. Registration is fast and FREE... Join Now!




C++, Integer and Long integer Literals

 
Reply to this topicStart new topic

C++, Integer and Long integer Literals, Adding L to the end of a Literal

Pourang
20 Jan, 2008 - 12:11 PM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 9


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

CODE
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
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C++, Integer And Long Integer Literals
20 Jan, 2008 - 12:31 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
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.

User is offlineProfile CardPM
+Quote Post

Pourang
RE: C++, Integer And Long Integer Literals
20 Jan, 2008 - 01:01 PM
Post #3

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 9


My Contributions
QUOTE(NickDMax @ 20 Jan, 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.


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?

:-)



User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C++, Integer And Long Integer Literals
20 Jan, 2008 - 02:07 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
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.

CODE

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

User is offlineProfile CardPM
+Quote Post

Pourang
RE: C++, Integer And Long Integer Literals
21 Jan, 2008 - 02:28 AM
Post #5

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 9


My Contributions
QUOTE(NickDMax @ 20 Jan, 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.

CODE

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


Thanks
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: C++, Integer And Long Integer Literals
23 Jan, 2008 - 05:47 AM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,026



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 01:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month