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

Discussion in 'C++' started by parag_paul@hotmail.com, Nov 3, 2007.

  1. Guest

    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.
     
    , Nov 3, 2007
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Nov 3, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    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.
     
    Ian Collins, Nov 3, 2007
    #3
  4. Rolf Magnus Guest

    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.
     
    Rolf Magnus, Nov 3, 2007
    #4
  5. Jack Klein Guest

    On 3 Nov 2007 01:14:31 -0700, ""
    <> 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
     
    Jack Klein, Nov 5, 2007
    #5
  6. dprody

    Joined:
    Oct 16, 2007
    Messages:
    4
    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.
     
    dprody, Nov 5, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,237
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,686
    Old Wolf
    Jan 20, 2004
  3. BogusException
    Replies:
    7
    Views:
    342
  4. Xiaozhong
    Replies:
    4
    Views:
    429
    Matthias Buelow
    Jan 23, 2008
  5. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,122
    David Thompson
    Jul 28, 2008
Loading...

Share This Page