Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is the need for long when int and long are both 4 bytes

Reply
Thread Tools

What is the need for long when int and long are both 4 bytes

 
 
parag_paul@hotmail.com
Guest
Posts: n/a
 
      11-03-2007
hi All
I understand the need for long long , but what is the purpose of long
as a data type separately. Just makes the language intimidating to
start with, when you have to deal with so many data types.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-03-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Re: What is the need for long when int and long are both 4 bytes


Where did you get that idea? Neither are required to be 4 bytes, and they
are not required to span the same range. The requirements are:

int can represent all integers in the interval [INT_MIN, INT_MAX].

long can represent all integers in the interval [LONG_MIN, LONG_MAX].

Moreover, we have the following guarantees inherited from C:

INT_MIN <= -32767
INT_MAX >= 32767

LONG_MIN <= -2147483647
LONG_MAX >= 2147483647


> hi All
> I understand the need for long long , but what is the purpose of long
> as a data type separately. Just makes the language intimidating to
> start with, when you have to deal with so many data types.


The requirements for the type int are somewhat special. If you look at
short, long, long long, you will find that they more or less match your
expectations. However, the idea behind int is that it should be
the "natural" arithmetic type of the machine (i.e., it should be
represented by one word). Now, what that means is a little iffy, e.g., do
you think of processor registers or of memory and bus architecture; but
that was the idea: in the word of the standard [3.9.1/2]:

... Plain ints have the natural size suggested by the architecture of the
execution environment [39]; the other signed integer types are provided to
meet special needs.

[39]= that is, large enough to contain any value in the range of INT_MIN
and INT_MAX, as defined in the header <climits>.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-03-2007
(E-Mail Removed) wrote:
> hi All
> I understand the need for long long , but what is the purpose of long
> as a data type separately. Just makes the language intimidating to
> start with, when you have to deal with so many data types.
>

Have you ever considered an 8 or 16 bit processor where int is 16 bits?
Or a 64 bit machine where int is 32 bits?

--
Ian Collins.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-03-2007
(E-Mail Removed) wrote:

> hi All
> I understand the need for long long ,


.... which doesn't exist in standard C++ ...

> but what is the purpose of long as a data type separately.


You need it if you want to be sure to get a type that has at least 32
siginificant bits. int doesn't give you that guarantee.

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-05-2007
On 3 Nov 2007 01:14:31 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> wrote in comp.lang.c++:

> hi All
> I understand the need for long long , but what is the purpose of long
> as a data type separately. Just makes the language intimidating to
> start with, when you have to deal with so many data types.


That's funny, I use an implementation where sizeof(int) is 1 and
sizeof(long) is 2. Are you sure your compiler isn't broken?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
dprody dprody is offline
Junior Member
Join Date: Oct 2007
Posts: 4
 
      11-05-2007
>That's funny, I use an implementation where sizeof(int) is 1 and
>sizeof(long) is 2. Are you sure your compiler isn't broken?



whats does sizeof(char) give on that compiler. most compilers i've seen the results of sizeof() are in bytes. and 1 byte is not large enough to meet the range required by the standard.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
In (int a[3]; int *p;) is a and p both GENUINE pointers to integers? Xiaozhong C++ 4 01-23-2008 02:54 PM
An int that is 4 bytes in native, and 12 bytes in .NET? Huh? BogusException C++ 7 02-06-2007 04:47 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments