Size of long

Discussion in 'C++' started by John Tsiombikas (Nuclear / the Lab), Jul 4, 2003.

  1. Nikolai Borissov wrote:
    > I know that C++ Standard does not impose any requirements for the size of
    > _long_ type. I wonder what is the size of _long_ in reality?


    What do you mean "in reality"? that depends on the implementation /
    system. Just do a cout << sizeof(long); and find out for your system.
    for my system, long is 4bytes.

    -- Nuclear / the Lab --
    John Tsiombikas (Nuclear / the Lab), Jul 4, 2003
    #1
    1. Advertising

  2. John Tsiombikas (Nuclear / the Lab)

    Magnus Guest

    "Nikolai Borissov" <> skrev i melding
    news:J93Na.1304$...
    > I know that C++ Standard does not impose any requirements for the size of
    > _long_ type. I wonder what is the size of _long_ in reality? I don't think
    > that there are architectures with 1-byte _long_. I think, the most

    probable
    > size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
    > architectures with that size?
    >
    > Thanks,
    >
    > Nikolai Borissov
    >
    >


    AFAIK you have no guarantee on the size of the long type in your case, what
    is guaranteed is the minimum size of this type, wich is 4 bytes for the long
    type. The int type is 2 bytes on some systems and 4 on others, you are
    however guaranteed that it is at the minimum 2 bytes.

    Could someone confirm this ?

    -Magnus
    Magnus, Jul 4, 2003
    #2
    1. Advertising

  3. John Tsiombikas (Nuclear / the Lab)

    Jim Fischer Guest

    Nikolai Borissov wrote:
    > Magnus wrote:
    >
    >
    >>AFAIK you have no guarantee on the size of the long type in your case,

    >
    > what
    >
    >>is guaranteed is the minimum size of this type, wich is 4 bytes for the

    >
    > long
    >
    >>type. The int type is 2 bytes on some systems and 4 on others, you are
    >>however guaranteed that it is at the minimum 2 bytes.

    >
    >
    > If minimum of 4 bytes is guaranteed by the Standard it would be excellent.
    > I've got only Draft Standard and couldn't find any specific information
    > about _long_ type. This whole point about portability.
    >
    >
    >>Could someone confirm this ?

    >
    >
    > Someone who is the true expert in the Standard, please respond.


    I don't claim to be a "true expert" in the C/C++ standards, but...

    The 'int' type must be able to represent values in the range INT_MIN to
    INT_MAX, as defined in the header <climits> (see: 3.9.1/2 and footnote
    39). The 'long int' type must provide *at least* as much storage as the
    'int' type (3.9.1/2).

    According to the C(99) standard [which is NOT the same C standard that
    C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
    equal or greater in magnitude (absolute value) to the following values,
    with the same sign:

    INT_MIN -32767
    INT_MAX +32767

    So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
    definitions are essentially the same as the C(99) definitions, then C++
    guarantees,

    2 <= sizeof(int) <= sizeof(long int)

    IOW, the guaranteed minimum size of a 'long int' object is 2 bytes, not
    4. At least, this is how I read the C(99)/C++(98) standards.

    FWIW, on 32-bit platforms, a "typical" standard C library implementation
    defines INT_MIN and INT_MAX so that (assuming 8-bit bytes),

    4 <= sizeof(int) <= sizeof(long int)

    But these are only "typical" values; they are not guaranteed values.

    Comments from the real C/C++ standard experts are welcome...

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
    Jim Fischer, Jul 4, 2003
    #3
  4. John Tsiombikas (Nuclear / the Lab)

    Mike Wahler Guest

    Nikolai Borissov <> wrote in message
    news:J93Na.1304$...
    > I know that C++ Standard does not impose any requirements for the size of
    > _long_ type.


    No you don't know that, since it's not true.
    Type 'long' is required to occupy at least 32 bits
    (but is allowed to be larger).

    > I wonder what is the size of _long_ in reality?


    It's different among implementations. You can use
    the 'sizeof' operator to determine the size of a
    particular type.

    sizeof(long)

    >I don't think
    > that there are architectures with 1-byte _long_.


    There indeed could be. Note that the size of a byte
    (char) is required to be at least eight bits, but
    could be more (e.g. 32 bits), in which case a 'long'
    might have size of one byte (but might not :))

    >I think, the most probable
    > size for _long_ would be 4 bytes.


    That's a common size for platforms with eight bit bytes.

    >What about 2-byte _long_, are there any
    > architectures with that size?


    There could be if the host platform used 16-bit bytes.

    Note that you can use the macros from <climits> or
    member functions of 'std::numeric_limits' from <limits>
    to determine the smallest/largest possible values for
    specific types on your implementation.

    -Mike
    Mike Wahler, Jul 4, 2003
    #4
  5. John Tsiombikas (Nuclear / the Lab)

    Mike Wahler Guest

    Magnus <> wrote in message
    news:3f04cab2$...
    >
    > "Nikolai Borissov" <> skrev i melding
    > news:J93Na.1304$...
    > > I know that C++ Standard does not impose any requirements for the size

    of
    > > _long_ type. I wonder what is the size of _long_ in reality? I don't

    think
    > > that there are architectures with 1-byte _long_. I think, the most

    > probable
    > > size for _long_ would be 4 bytes. What about 2-byte _long_, are there

    any
    > > architectures with that size?
    > >
    > > Thanks,
    > >
    > > Nikolai Borissov
    > >
    > >

    >
    > AFAIK you have no guarantee on the size of the long type in your case,

    what
    > is guaranteed is the minimum size of this type, wich is 4 bytes for the

    long
    > type. The int type is 2 bytes on some systems and 4 on others, you are
    > however guaranteed that it is at the minimum 2 bytes.
    >
    > Could someone confirm this ?


    I can 'disconfirm' it. :)

    There's no requirement that the size of a byte not
    be more than eight bits. E.g. a platform with sixteen
    bit bytes can have a conforming type 'long' which is
    only two bytes in size.

    -Mike
    Mike Wahler, Jul 4, 2003
    #5
  6. John Tsiombikas (Nuclear / the Lab)

    Mike Wahler Guest

    Nikolai Borissov <> wrote in message
    news:R74Na.1328$...
    > Magnus wrote:
    >
    > > AFAIK you have no guarantee on the size of the long type in your case,

    > what
    > > is guaranteed is the minimum size of this type, wich is 4 bytes for the

    > long
    > > type. The int type is 2 bytes on some systems and 4 on others, you are
    > > however guaranteed that it is at the minimum 2 bytes.

    >
    > If minimum of 4 bytes is guaranteed by the Standard it would be excellent.


    First, drop the assumption that all bytes are eight bits.
    They're not. The standard does require a byte have
    *at least* eight bits, but allows more.

    The smallest allowed size for type 'long' is sixteen bits.
    However many bytes that takes on the host platform, depends
    upon that platform.

    > I've got only Draft Standard and couldn't find any specific information
    > about _long_ type. This whole point about portability.


    I think you need to look harder. :)
    All the types' requirements are specified.

    >
    > > Could someone confirm this ?


    Nope. Can't confirm what ain't true. :)


    >
    > Someone who is the true expert in the Standard, please respond.


    No such person exists imo.

    -Mike
    Mike Wahler, Jul 4, 2003
    #6
  7. I know that C++ Standard does not impose any requirements for the size of
    _long_ type. I wonder what is the size of _long_ in reality? I don't think
    that there are architectures with 1-byte _long_. I think, the most probable
    size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
    architectures with that size?

    Thanks,

    Nikolai Borissov
    Nikolai Borissov, Jul 4, 2003
    #7
  8. Magnus wrote:

    > AFAIK you have no guarantee on the size of the long type in your case,

    what
    > is guaranteed is the minimum size of this type, wich is 4 bytes for the

    long
    > type. The int type is 2 bytes on some systems and 4 on others, you are
    > however guaranteed that it is at the minimum 2 bytes.


    If minimum of 4 bytes is guaranteed by the Standard it would be excellent.
    I've got only Draft Standard and couldn't find any specific information
    about _long_ type. This whole point about portability.

    > Could someone confirm this ?


    Someone who is the true expert in the Standard, please respond.

    Nikolai Borissov
    Nikolai Borissov, Jul 4, 2003
    #8
  9. John Tsiombikas (Nuclear / the Lab)

    Jack Klein Guest

    On Thu, 3 Jul 2003 19:41:02 -0700, "Nikolai Borissov"
    <> wrote in comp.lang.c++:

    > I know that C++ Standard does not impose any requirements for the size of
    > _long_ type. I wonder what is the size of _long_ in reality? I don't think
    > that there are architectures with 1-byte _long_. I think, the most probable
    > size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
    > architectures with that size?
    >
    > Thanks,
    >
    > Nikolai Borissov


    Sigh, another desk-top chauvinist. You think the platforms you work
    on are all there is, whereas C and C++ are quite extensively used in
    embedded systems using processor architectures you have never heard
    of.

    I am literally writing code for a Texas Instruments 2812 16/32 bit
    digital signal processor which comes with (subset) free-standing C and
    C++ compilers.

    This processor can't access memory in anything smaller than 16 bit
    words. So the character types are 1 byte (but have 16 bits). The
    short and int types are also 1 byte (with 16 bits). And signed and
    unsigned long are 2 bytes long and have 32 bits.

    And there are 32 bit DSPs (from TI, AD, and probably Motorola) where
    everything is 32 bits, char, short, int, and long. So sizeof(long) ==
    1.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Jul 4, 2003
    #9
  10. John Tsiombikas (Nuclear / the Lab)

    Pete Becker Guest

    Jim Fischer wrote:
    >
    > According to the C(99) standard [which is NOT the same C standard that
    > C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
    > equal or greater in magnitude (absolute value) to the following values,
    > with the same sign:
    >
    > INT_MIN -32767
    > INT_MAX +32767
    >
    > So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
    > definitions are essentially the same as the C(99) definitions, then C++
    > guarantees,
    >


    That was true in the C90 standard, so it applies in C++ as well. But
    there's something even more useful: LONG_MIN and LONG_MAX are also
    specified, and impose a minimum size of 32 bits.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 4, 2003
    #10
  11. John Tsiombikas (Nuclear / the Lab)

    Jerry Coffin Guest

    In article <J93Na.1304$>,
    says...
    > I know that C++ Standard does not impose any requirements for the size of
    > _long_ type. I wonder what is the size of _long_ in reality? I don't think
    > that there are architectures with 1-byte _long_. I think, the most probable
    > size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
    > architectures with that size?


    A long must be a minimum of 32 bits. A byte must be a minimum of 8
    bits, but may be more, so if long is 32 bits, it can be anywhere from 1
    to 4 bytes. If long is larger than, more bits can be more bytes (still
    subject to the 8 bits per byte minimum) so, for example, a 64-bit long
    could be anywhere from 1 to 8 bytes.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jul 4, 2003
    #11
  12. John Tsiombikas (Nuclear / the Lab)

    Mike Wahler Guest

    Rolf Magnus <> wrote in message
    news:be3kl7$dcp$03$-online.com...
    > Mike Wahler wrote:
    >
    > > The smallest allowed size for type 'long' is sixteen bits.

    >
    > No, it's 32.


    Um, yes, that's what I meant to say (see my other posts
    this thread). Sorry for the typo.

    -Mike
    Mike Wahler, Jul 4, 2003
    #12
  13. John Tsiombikas (Nuclear / the Lab)

    Jim Fischer Guest

    Pete Becker wrote:
    > Jim Fischer wrote:
    >
    >>According to the C(99) standard [which is NOT the same C standard that
    >>C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
    >>equal or greater in magnitude (absolute value) to the following values,
    >>with the same sign:
    >>
    >> INT_MIN -32767
    >> INT_MAX +32767
    >>
    >>So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
    >>definitions are essentially the same as the C(99) definitions, then C++
    >>guarantees,
    >>

    >
    >
    > That was true in the C90 standard, so it applies in C++ as well. But
    > there's something even more useful: LONG_MIN and LONG_MAX are also
    > specified, and impose a minimum size of 32 bits.


    So what is the connection between the C++ standard and the LONG_MIN,
    LONG_MAX values in the C standard? What I mean is, the C++ standard
    specifically mentions C's INT_MIN, INT_MAX values WRT C++'s 'int' type
    (see 3.9.1/2 and footnote 39). Does the C++ standard impose a similar
    requirement that the C++ 'long int' type be able to represent (at
    least?) the same range of values as the C language's LONG_MIN, LONG_MAX
    values?

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
    Jim Fischer, Jul 4, 2003
    #13
  14. John Tsiombikas (Nuclear / the Lab)

    Jim Fischer Guest

    Pete Becker wrote:
    > Jim Fischer wrote:
    >
    >>So what is the connection between the C++ standard and the LONG_MIN,
    >>LONG_MAX values in the C standard? What I mean is, the C++ standard
    >>specifically mentions C's INT_MIN, INT_MAX values WRT C++'s 'int' type
    >>(see 3.9.1/2 and footnote 39). Does the C++ standard impose a similar
    >>requirement that the C++ 'long int' type be able to represent (at
    >>least?) the same range of values as the C language's LONG_MIN, LONG_MAX
    >>values?
    >>

    >
    >
    > See 18.2.2/1 [lib.c.limits], which incorporates the C limits macros
    > through <climits>. It incorporates the indirection that's in the C
    > standard: the macros specify the actual range of representable values,
    > along with minimum requirements that must be met.


    Ah, of course. <forehead smack> Thanks, Pete.

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
    Jim Fischer, Jul 4, 2003
    #14
  15. John Tsiombikas (Nuclear / the Lab)

    Jack Klein Guest

    On Fri, 4 Jul 2003 04:11:22 -0700, "Nikolai Borissov"
    <> wrote in comp.lang.c++:

    > Jack Klein wrote:
    >
    > > Sigh, another desk-top chauvinist. You think the platforms you work
    > > on are all there is, whereas C and C++ are quite extensively used in
    > > embedded systems using processor architectures you have never heard
    > > of.

    >
    > Well, I'm not a desk-top chauvinist, otherwise I wouldn't have bothered at
    > all.
    >
    > > I am literally writing code for a Texas Instruments 2812 16/32 bit
    > > digital signal processor which comes with (subset) free-standing C and
    > > C++ compilers.
    > >
    > > This processor can't access memory in anything smaller than 16 bit
    > > words. So the character types are 1 byte (but have 16 bits). The
    > > short and int types are also 1 byte (with 16 bits). And signed and
    > > unsigned long are 2 bytes long and have 32 bits.
    > >
    > > And there are 32 bit DSPs (from TI, AD, and probably Motorola) where
    > > everything is 32 bits, char, short, int, and long. So sizeof(long) ==
    > > 1.

    >
    > Actually, I was a bit inaccurate in my original question, because what I'm
    > concerned with is not a _sizeof_ value of type _long_, rather number of bits
    > in the type. Most of the calculations in my program require only 16-bit size
    > variables, but in some cases 32-bit ones are necessary. For 16-bit
    > calculations I use type _int_, for 32-bit type _long_, even though they are
    > of the same 32-bit size on my desktop. I'm deliberately using type _long_
    > with the portability concern in mind. The good news in your post is that the
    > 2 imbedded processors you mentioned do have 32-bit long.
    >
    > Cheers,
    > Nikolai Borissov


    One possibility is to use a version of the latest C standard's
    <inttypes.h>, which is likely to make it into the next update to the
    C++ standard.

    Almost all platforms have integer types that meet the requirements
    (with the exception of those DSPs I mentioned above), so you can
    tailor it accordingly.

    That way you can use int16_t when you want a value that can hold 16
    bits, and int32_t when you need more range.

    On a typical 32 bit compiler, int32_t would be a typedef for int, on a
    16 bit compiler, a typedef for long.

    Likewise int16_t would be a typedef for int on a compiler with 16 bit
    ints, and for short on a typical 32 bit desk top compiler.

    The C++ standard specifies the minimum ranges of values that each of
    the integer types must hold, and it inherits those values from C.
    Given that a pure binary notation is required for the integer types,
    it is easy to work out the minimum number of bits each type must
    contain:

    8 bits for the character types

    16 bits for short and int

    32 bits for long

    64 bits for long long (another C99 feature likely to make in into
    the next update of the C++ standard)

    So on any conforming C or C++ implementation, long must have at least
    32 bits.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Jul 5, 2003
    #15
  16. John Tsiombikas (Nuclear / the Lab)

    Rolf Magnus Guest

    Jack Klein wrote:

    > One possibility is to use a version of the latest C standard's
    > <inttypes.h>, which is likely to make it into the next update to the
    > C++ standard.
    >
    > Almost all platforms have integer types that meet the requirements
    > (with the exception of those DSPs I mentioned above), so you can
    > tailor it accordingly.


    Well, only the fixed size types might not be present.

    > That way you can use int16_t when you want a value that can hold 16
    > bits, and int32_t when you need more range.


    I'd rather use int_fast16_t or int_least16_t if I need not more than 16
    bits, unless there is a really good reason to need _exactly_ 16 bits.
    This will also work on the DSPs you mentioned (since those are also
    defined if there is no type with exactly 16 bits), and it should,
    depending on which you choose, use the smallest possible or the fastest
    possible type with at least 16 bits. So if you want to create a huge
    array and save memory, you can choose the int_least16_t, and for single
    values, int_fast16_t.
    Rolf Magnus, Jul 5, 2003
    #16
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    662
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,179
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    463
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    784
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,779
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page