Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 11th, 2007, 11:25 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default What's the difference between an int and a long

Can someone please explain the real difference between an int and a long? I am reading Visual C++ 2005 by Ivor he describes both as 4 bytes in size and have a range of -2,147,483,648 to 2,147,483,647 I am referring thus to ISO/ANSI C++. Any help would be appreciated.

Reply With Quote
  #2 (permalink)  
Old February 13th, 2007, 09:00 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

Well the difference is very small but can be very important in some instances. When programming the second most important objective (after making the program do what it is supposed to) is saving memory to make the program run as fast as possible. This is where your keyword "long" comes in. long is an integer but integer isn't exactly a long understand? There is also a "short". now usually these are referenced with int after them but it is not required. The main difference between them is their size. Depending on the computer but in most cases a "short" is two bytes a "long" is four bytes and an "int" can be two or four bytes. It is important in programs that you declare it short or long if you know exactly how big the number is going to be and how often it is going to modified so that you can save as much memory as possible. Now as far as the range that depends on the size of the int whether it is two or four bytes. Here is a table to reference sizes and values.

type size range
short int 2 bytes -32,768 to 32,767
long int 4 bytes -2,147,483,648 to 2,147,483,647
int (16 bit) 2 bytes -32,768 to 32,767
int (32 bit) 4 bytes -2,147,483,648 to 2,147,483,647

There are also signed and unsigned if you need help with those just ask but they are really simple

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote
  #3 (permalink)  
Old February 20th, 2007, 12:15 AM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Geo.

Based on your answer, is it safe to say the size of a long depends on how it is implemented by a compiler' vendor? meaning on Microsoft Visual C++ it is implemented as 4 bytes on another vendor's compiler it could be 8 bytes?

Reply With Quote
  #4 (permalink)  
Old February 20th, 2007, 08:42 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

No problem!

However the compiler isn't exactly what changes it it is actually the computer it's self I'm not exactly sure but there may be a relationship between the compiler and the size but I have never heard anything about it and have never considered it but you have inspired me to test it out =) you should try it yourself =)

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote
  #5 (permalink)  
Old March 4th, 2007, 01:29 AM
Registered User
 
Join Date: Mar 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The underlying CPU architecture plays an important role.
for e.g on a 64bit cpu the, size of int would be , 64bits i.e 8bytes.


Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference bet Convert.Toint32 and Int.Parse anujrathi General .NET 3 September 8th, 2007 02:31 AM
Long Long int to bin walid C# 0 January 23rd, 2007 12:47 PM
Double in Long int walid C# 0 January 20th, 2007 08:50 AM
Use of long long int jvanp C# 2005 2 September 20th, 2006 06:07 PM
Use of long long int jvanp C++ Programming 7 October 3rd, 2004 01:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.