difference between int and long

Discussion in 'C++' started by Imran, Jan 16, 2007.

  1. Imran

    Imran Guest

    hello all

    I am bit confused with int and long data type.

    I read in some book that, int cal hold 2^16 values
    where in long can hold 2^32. and both data types are 4 bytes long.

    My doubt this, since both are 4 bytes , how can it hold different size?

    Thank you
     
    Imran, Jan 16, 2007
    #1
    1. Advertising

  2. Imran

    Ondra Holub Guest

    Imran napsal:
    > hello all
    >
    > I am bit confused with int and long data type.
    >
    > I read in some book that, int cal hold 2^16 values
    > where in long can hold 2^32. and both data types are 4 bytes long.
    >
    > My doubt this, since both are 4 bytes , how can it hold different size?
    >
    > Thank you


    Standard guarantees only that int is at least 16-bit and long is at
    least 32-bit and sizeof(int) <= sizeof(long).

    So for example on typical 32-bit platform are int and long both 32-bit,
    on 64-bit Linux can be int 32-bit and long 64-bit etc.
     
    Ondra Holub, Jan 16, 2007
    #2
    1. Advertising

  3. Imran wrote:
    > hello all
    >
    > I am bit confused with int and long data type.
    >
    > I read in some book that, int cal hold 2^16 values
    > where in long can hold 2^32. and both data types are 4 bytes long.
    >
    > My doubt this, since both are 4 bytes , how can it hold different size?
    >
    > Thank you
    >
    >


    If they are both 4 bytes then they can both hold 2^32. The difference
    between int and long depends on the type of processor. On many
    processors today they are exactly the same.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Jan 16, 2007
    #3
  4. Imran

    Philipp Reh Guest

    On Tue, 16 Jan 2007 10:30:40 -0500, Scott McPhillips [MVP] wrote:

    > Imran wrote:
    >> hello all
    >>
    >> I am bit confused with int and long data type.
    >>
    >> I read in some book that, int cal hold 2^16 values
    >> where in long can hold 2^32. and both data types are 4 bytes long.
    >>
    >> My doubt this, since both are 4 bytes , how can it hold different size?
    >>
    >> Thank you
    >>
    >>

    >
    > If they are both 4 bytes then they can both hold 2^32. The difference
    > between int and long depends on the type of processor. On many
    > processors today they are exactly the same.
    >


    That is not true. C++ defines a byte in terms of the size of a character
    which must be at least 8 bits.
    The meaning of "4 bytes" thus is 4 times the size of char which doesn't
    have to equal 32 bits in size.
     
    Philipp Reh, Jan 16, 2007
    #4
  5. Imran

    Grizlyk Guest

    Philipp Reh write:

    > > If they are both 4 bytes then they can both hold 2^32. The difference
    > > between int and long depends on the type of processor. On many
    > > processors today they are exactly the same.
    > >

    >
    > That is not true. C++ defines a byte in terms of the size of a character
    > which must be at least 8 bits.
    > The meaning of "4 bytes" thus is 4 times the size of char which doesn't
    > have to equal 32 bits in size.


    It seems to me, that there is no keyword "byte" in C++ programs.
    Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
    size of "byte" is more useful, because a man can understand concrete
    size of memory in bits. I think "byte" is language independent system
    independent memory size, similar to "bit".

    But C++ "char" is not a "byte". C++ "char" is minimal system depended
    granularity (part) of memory,
    a) char by char describe continuous memory ( memory without holes and
    lost bits )
    b) char is suitable for all other types
    1) as align bound of the type
    2) number of chars per the type is integral number (not float)
    c) sizeof() of char is always "1"
    d) each type has MAX_SIZE according only to memory size - if "char" is
    16 bit, char can hold values 0x0000 - 0xffff, not 0x00 - 0xff.
    e) 8bit<=char<=short<=int<=long

    On x86 "char" number of "bits" is equal to "byte" number of "bits".

    The reason to declare type with sizeof==2, aligned in memory to
    sizeof==4 boundary is allow packed storage in external memory or packed
    structures, aligned to sizeof==2.
     
    Grizlyk, Jan 17, 2007
    #5
  6. * Grizlyk:
    > Philipp Reh write:
    >
    >>> If they are both 4 bytes then they can both hold 2^32. The difference
    >>> between int and long depends on the type of processor. On many
    >>> processors today they are exactly the same.
    >>>

    >> That is not true. C++ defines a byte in terms of the size of a character
    >> which must be at least 8 bits.
    >> The meaning of "4 bytes" thus is 4 times the size of char which doesn't
    >> have to equal 32 bits in size.

    >
    > It seems to me, that there is no keyword "byte" in C++ programs.


    That's right, but in the C++ standard and C++ context (or C, for that
    matter) 'byte' means 'char' and vice versa.


    > Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
    > size of "byte" is more useful, because a man can understand concrete
    > size of memory in bits.


    Yep, that's the old "eat dung" argument, that twenty zillion flies can't
    be wrong. Yet, while they're undoubtedly doing what's right for them,
    and so aren't wrong about eating that dung, the inference that the same
    behavior is appropriate for, say, humans, is incorrect. There's more
    than one meaning of 'byte' -- after all, language evolves -- and in a
    technical context where the number of bits matters the 8 bit entity is
    more properly and precisely called an 'octet' (mostly this is used by
    electrical engineers and the telecommunications/network industry).


    > I think "byte" is language independent system
    > independent memory size, similar to "bit".


    Not in the context of C++. Look it up at NIST, IEC or really anywhere.
    Outside the context of C++, it is a /system dependent/ size. Which is
    why 'octet' is used when it's necessary to be precise.


    > But C++ "char" is not a "byte". C++ "char" is minimal system depended
    > granularity (part) of memory,


    Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
    storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
    copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
    char is 1).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 17, 2007
    #6
  7. Imran

    Mike Wahler Guest

    "Grizlyk" <> wrote in message
    news:...
    > Philipp Reh write:
    >
    >> > If they are both 4 bytes then they can both hold 2^32. The difference
    >> > between int and long depends on the type of processor. On many
    >> > processors today they are exactly the same.
    >> >

    >>
    >> That is not true. C++ defines a byte in terms of the size of a character
    >> which must be at least 8 bits.
    >> The meaning of "4 bytes" thus is 4 times the size of char which doesn't
    >> have to equal 32 bits in size.

    >
    > It seems to me, that there is no keyword "byte" in C++ programs.


    No, the keyword is 'char'. The standard (IEC 14882) defines 'char'
    as being a byte.

    > Outside of C++, most people treat bit as bit


    A bit is a bit.

    >and byte as 8 bit - fixed


    That's not always true. The proper term for an eight-bit object
    is 'octet'.

    > size of "byte" is more useful, because a man can understand concrete
    > size of memory in bits.


    Except that each platform defines what 'concrete' is. :)

    > I think "byte" is language independent system


    It's platform dependent. Some use different size bytes than others.
    that's why we have the 'CHAR_BIT' macro (and
    std::numeric_limits<char>::digits)

    > independent memory size, similar to "bit".
    >
    > But C++ "char" is not a "byte". C++ "char" is minimal system depended
    > granularity (part) of memory,


    Both C and C++ define 'char' as meaning *exactly* 'byte', whatever
    the size of a byte on the host platform happens to be. However, both
    languages require that the size of a char (i.e. byte) be at least eight
    bits.

    So on any system, regardless of how many bits in a byte, sizeof(char) == 1.
    Always.

    -Mike
     
    Mike Wahler, Jan 17, 2007
    #7
  8. Imran

    Grizlyk Guest

    Alf P. Steinbach wrote:

    > Yep, that's the old "eat dung" argument, that twenty zillion flies can't


    No, it is not "eat dung". Ii is standard international (SI) measure of
    information or storage capacity, used by most people in practical
    cases, similarly to meter or second.

    > > But C++ "char" is not a "byte". C++ "char" is minimal system depended
    > > granularity (part) of memory,

    >
    > Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
    > storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
    > copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
    > char is 1).


    You are right formally, but fortunatelly, the part of standard can be
    silently ignored at practical cases, as well as we can silently ignore
    existence of keyword "goto". I am shure, if you will describe storage
    capacity elsewere out of C++ context, much better for all (and for you
    too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
    "13-bit C++ bytes", if you want, that your opponent can understand what
    did you say.

    And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
    - C++ specific memory size as base for all types.
     
    Grizlyk, Jan 17, 2007
    #8
  9. Imran

    Grizlyk Guest

  10. * Grizlyk:
    > Alf P. Steinbach wrote:
    >
    >> Yep, that's the old "eat dung" argument, that twenty zillion flies can't

    >
    > No, it is not "eat dung". Ii is standard international (SI) measure of
    > information or storage capacity, used by most people in practical
    > cases, similarly to meter or second.
    >
    >>> But C++ "char" is not a "byte". C++ "char" is minimal system depended
    >>> granularity (part) of memory,

    >> Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
    >> storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
    >> copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
    >> char is 1).

    >
    > You are right formally, but fortunatelly, the part of standard can be
    > silently ignored at practical cases, as well as we can silently ignore
    > existence of keyword "goto". I am shure, if you will describe storage
    > capacity elsewere out of C++ context, much better for all (and for you
    > too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
    > "13-bit C++ bytes", if you want, that your opponent can understand what
    > did you say.
    >
    > And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
    > - C++ specific memory size as base for all types.


    Manual Plonk.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 17, 2007
    #10
  11. Imran

    Kai-Uwe Bux Guest

    Grizlyk wrote:

    >
    > Alf P. Steinbach wrote:
    >
    >> Yep, that's the old "eat dung" argument, that twenty zillion flies can't

    >
    > No, it is not "eat dung". Ii is standard international (SI) measure of
    > information or storage capacity, used by most people in practical
    > cases, similarly to meter or second.
    >
    >> > But C++ "char" is not a "byte". C++ "char" is minimal system depended
    >> > granularity (part) of memory,

    >>
    >> Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
    >> storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
    >> copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
    >> char is 1).

    >
    > You are right formally, but fortunatelly, the part of standard can be
    > silently ignored at practical cases, as well as we can silently ignore
    > existence of keyword "goto".


    Actually, we cannot ignore either of those in this group: neither for
    practical purposes nor (and more importantly) for discussing C++.

    > I am shure, if you will describe storage
    > capacity elsewere out of C++ context, much better for all (and for you
    > too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
    > "13-bit C++ bytes", if you want, that your opponent can understand what
    > did you say.


    Sure. It is somewhat unfortunate that the C++ standard is intruding quite a
    bit into the ordinary English language by specializing the meanings of
    common terms such
    as "exception", "object", "undefined" / "unspecified" / "implementation
    defined" (behavior) or, in this case, "byte". However, communication in
    this group is greatly simplified if everybody just sticks to two simple
    conventions:

    a) If the standard defines the meaning of a term, do not use that term with
    a different meaning without explicitly indicating so.

    b) If the standard provides a term for a concept and you mean that concept,
    use the terminology used in the standard.

    As these two rules of thumb have proven quite useful, regulars tend to be
    quite opinionate about breaking those rules willfully.

    Note that I am not arguing that you should not use "byte" in its ordinary
    meaning outside this group. But within this group, it has proven useful to
    avoid confusion arising from competing meanings. The resolution of such
    competition is: the meaning used in the standard wins.


    > And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
    > - C++ specific memory size as base for all types.


    That "byte" is not a keyword does not make it any less C++ specific
    terminology as, say, "object", which is also not a keyword.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 17, 2007
    #11
  12. Imran

    Grizlyk Guest

    Kai-Uwe Bux wrote:

    > Grizlyk wrote:
    >
    > > I am shure, if you will describe storage
    > > capacity elsewere out of C++ context, much better for all (and for you
    > > too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
    > > "13-bit C++ bytes", if you want, that your opponent can understand what
    > > did you say.

    >
    > Sure. It is somewhat unfortunate that the C++ standard is intruding quite a
    > bit into the ordinary English language by specializing the meanings of
    > common terms such
    > as "exception", "object", "undefined" / "unspecified" / "implementation
    > defined" (behavior) or, in this case, "byte". However, communication in
    > this group is greatly simplified if everybody just sticks to two simple
    > conventions:


    Are you really do not see defferences between SI definitions and other
    "words" in the world?

    Well, maybe "byte" is not defined in SI, but the fixed size of byte (8
    bit) is conventional for most peolpe in the world. And no one "cometee"
    of any language can not force people to have more than one "byte" with
    different meaning, as no one "cometee" can not introduce two types of
    "dollar": "ordinary dollar"- 100-cents, and "C++ dollar" - with
    variable number of cents, for example 315 cents.

    Especially, when C++ has own replacement for "C++ specific byte" - it
    is "char". There is no sence to have in C++ "byte" definition (which
    constradiscts to ordinary "byte" definition), because "char" exist.

    To discuss "to define 'C++ specific byte' or do not" is time wasted.
     
    Grizlyk, Jan 19, 2007
    #12
  13. Imran

    Grizlyk Guest

    Grizlyk wrote:
    >
    > e) 8bit<=char<=short<=int<=long
    >

    Accordance to "The design and evolution of C++"
    e) 8bit<=char<=short<=16bit<=int<=long
     
    Grizlyk, Jan 19, 2007
    #13
  14. Imran

    Kai-Uwe Bux Guest

    Grizlyk wrote:

    >
    > Kai-Uwe Bux wrote:
    >
    >> Grizlyk wrote:
    >>
    >> > I am shure, if you will describe storage
    >> > capacity elsewere out of C++ context, much better for all (and for you
    >> > too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
    >> > "13-bit C++ bytes", if you want, that your opponent can understand what
    >> > did you say.

    >>
    >> Sure. It is somewhat unfortunate that the C++ standard is intruding quite
    >> a bit into the ordinary English language by specializing the meanings of
    >> common terms such
    >> as "exception", "object", "undefined" / "unspecified" / "implementation
    >> defined" (behavior) or, in this case, "byte". However, communication in
    >> this group is greatly simplified if everybody just sticks to two simple
    >> conventions:

    >
    > Are you really do not see defferences between SI definitions and other
    > "words" in the world?
    >
    > Well, maybe "byte" is not defined in SI, but the fixed size of byte (8
    > bit) is conventional for most peolpe in the world.


    True and already conceded.

    > And no one "cometee"
    > of any language can not force people to have more than one "byte" with
    > different meaning, as no one "cometee" can not introduce two types of
    > "dollar": "ordinary dollar"- 100-cents, and "C++ dollar" - with
    > variable number of cents, for example 315 cents.


    Actually, it can. In the very same way that mathematicians have overloaded
    the terms "group", "ring", "field", "loop", etc. Once introduced by the
    standard, the specialized meanings are default meanings In This News Group.

    > Especially, when C++ has own replacement for "C++ specific byte" - it
    > is "char". There is no sence to have in C++ "byte" definition (which
    > constradiscts to ordinary "byte" definition), because "char" exist.
    >
    > To discuss "to define 'C++ specific byte' or do not" is time wasted.


    Whatever.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 19, 2007
    #14
    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. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,248
    Peter Shaggy Haywood
    Sep 20, 2005
  2. arun
    Replies:
    8
    Views:
    485
    Dave Thompson
    Jul 31, 2006
  3. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,187
    David Thompson
    Jul 28, 2008
  4. veryhotsausage
    Replies:
    1
    Views:
    1,902
    veryhotsausage
    Jul 4, 2008
  5. Oliver Graeser
    Replies:
    10
    Views:
    607
    Oliver Graeser
    Sep 26, 2008
Loading...

Share This Page