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

P

parag_paul

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

Kai-Uwe Bux

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
 
I

Ian Collins

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?
 
R

Rolf Magnus

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

Jack Klein

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
 
Joined
Oct 16, 2007
Messages
4
Reaction score
0
sizeof(char)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top