sizeof C integral types

Discussion in 'C Programming' started by ark, Jan 14, 2004.

  1. ark

    ark Guest

    Risking to invoke flames from one Tom St Denis of Ottawa :)

    Is there any guarantee that, say,
    sizeof(int) == sizeof(unsigned int)
    sizeof(long) > sizeof(char) ?

    Thanks,
    Ark
     
    ark, Jan 14, 2004
    #1
    1. Advertising

  2. ark wrote:
    >
    > Is there any guarantee that, say,
    > sizeof(int) == sizeof(unsigned int)
    > sizeof(long) > sizeof(char) ?


    I don't believe the standard guarantees that either is true.

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
     
    Russell Hanneken, Jan 14, 2004
    #2
    1. Advertising

  3. ark wrote:

    > Is there any guarantee that, say,
    > sizeof(int) == sizeof(unsigned int)


    Yes.

    > sizeof(long) > sizeof(char) ?


    No. BTW, sizeof(char) == 1 by definition.

    However, sizeof(long) == 1, which I think implies sizeof(int) == 1,
    would break several very common idioms, e.g.

    int ch;
    while ((ch = getchar()) != EOF) { ... }

    because EOF is supposed to be a value which is different from all
    'unsigned char' values. That is only possible when 'int' is wider
    than 'unsigned char'.

    Personally I've never seen a program which worred about this
    possibility, though I suppose such programs exist. It might
    be different with freestanding implementations (implementations
    which do not use the C library, so getchar() is no problem).

    --
    Hallvard
     
    Hallvard B Furuseth, Jan 14, 2004
    #3
  4. ark

    Ben Pfaff Guest

    "ark" <> writes:

    > Is there any guarantee that, say,
    > sizeof(int) == sizeof(unsigned int)


    Yes.

    > sizeof(long) > sizeof(char) ?


    No.
    --
    "Your correction is 100% correct and 0% helpful. Well done!"
    --Richard Heathfield
     
    Ben Pfaff, Jan 14, 2004
    #4
  5. ark

    Ben Pfaff Guest

    Russell Hanneken <> writes:

    > ark wrote:
    >> Is there any guarantee that, say,
    >> sizeof(int) == sizeof(unsigned int)
    >> sizeof(long) > sizeof(char) ?

    >
    > I don't believe the standard guarantees that either is true.


    The former, but not the latter, is guaranteed.
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Ben Pfaff, Jan 14, 2004
    #5
  6. ark wrote:

    > Risking to invoke flames from one Tom St Denis of Ottawa :)
    >
    > Is there any guarantee that, say,
    > sizeof(int) == sizeof(unsigned int)
    > sizeof(long) > sizeof(char)?


    Are you aware of the type definitions in <stdint.h>?
     
    E. Robert Tisdale, Jan 14, 2004
    #6
  7. ark

    Ben Pfaff Guest

    Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> writes:

    > However, sizeof(long) == 1, which I think implies sizeof(int) == 1,


    Actually there's no such implication. The range of int is a
    subrange of the range of long, but there's no such guarantee on
    the size in bytes of these types.

    However, it would be a strange system for which sizeof(long) <
    sizeof(int). I don't know of any.
    --
    "...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
    in the uncertainty principle. Which of course makes the above equivalent to
    Schrodinger's pointer..."
    --Anthony McDonald
     
    Ben Pfaff, Jan 14, 2004
    #7
  8. ark

    ark Guest

    "Hallvard B Furuseth" <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote in
    message news:...
    >
    > However, sizeof(long) == 1, which I think implies sizeof(int) == 1,
    > would break several very common idioms, e.g.
    >
    > int ch;
    > while ((ch = getchar()) != EOF) { ... }
    >
    > because EOF is supposed to be a value which is different from all
    > 'unsigned char' values. That is only possible when 'int' is wider
    > than 'unsigned char'.
    >
    > Personally I've never seen a program which worred about this
    > possibility, though I suppose such programs exist. It might
    > be different with freestanding implementations (implementations
    > which do not use the C library, so getchar() is no problem).
    >
    > --
    > Hallvard


    I believe that a 16-bit DSP with a 16-bit byte would have
    sizeof(int)==sizeof(short) (and ==1).
    - Ark
     
    ark, Jan 14, 2004
    #8
  9. ark

    CBFalconer Guest

    "E. Robert Tisdale" wrote:
    > ark wrote:
    >
    > > Risking to invoke flames from one Tom St Denis of Ottawa :)
    > >
    > > Is there any guarantee that, say,
    > > sizeof(int) == sizeof(unsigned int)
    > > sizeof(long) > sizeof(char)?

    >
    > Are you aware of the type definitions in <stdint.h>?


    Only implied misinformation from Trollsdale this time. <stdint.h>
    is a C99 artifact, and the type defined therein are only defined
    when the implementation has suitable types. So you could do
    something like:

    #if defined(sometype)
    #define mytype sometype
    #else
    #define mytype whatever
    #endif

    with suitable guards for a C99 system.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jan 15, 2004
    #9
  10. In article <>,
    "E. Robert Tisdale" <> wrote:

    > ark wrote:
    >
    > > Risking to invoke flames from one Tom St Denis of Ottawa :)
    > >
    > > Is there any guarantee that, say,
    > > sizeof(int) == sizeof(unsigned int)
    > > sizeof(long) > sizeof(char)?

    >
    > Are you aware of the type definitions in <stdint.h>?


    Quite possibly he is aware of them and knows that they have nothing to
    do with the question asked.
     
    Christian Bau, Jan 15, 2004
    #10
  11. ark

    Jack Klein Guest

    On Thu, 15 Jan 2004 01:08:41 GMT, CBFalconer <>
    wrote in comp.lang.c:

    > "E. Robert Tisdale" wrote:
    > > ark wrote:
    > >
    > > > Risking to invoke flames from one Tom St Denis of Ottawa :)
    > > >
    > > > Is there any guarantee that, say,
    > > > sizeof(int) == sizeof(unsigned int)
    > > > sizeof(long) > sizeof(char)?

    > >
    > > Are you aware of the type definitions in <stdint.h>?

    >
    > Only implied misinformation from Trollsdale this time. <stdint.h>
    > is a C99 artifact, and the type defined therein are only defined
    > when the implementation has suitable types. So you could do
    > something like:
    >
    > #if defined(sometype)
    > #define mytype sometype
    > #else
    > #define mytype whatever
    > #endif
    >
    > with suitable guards for a C99 system.


    It is actually quite possible, and very useful, to build a subset of
    <stdint.h> for any compiler. Interestingly enough, the (complete, not
    subset) <stdint.h> that comes with ARM's ADS compiles and works
    perfectly with Visual C++ 6.

    --
    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++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 15, 2004
    #11
  12. ark

    CBFalconer Guest

    Jack Klein wrote:
    >

    .... snip ...
    >
    > It is actually quite possible, and very useful, to build a subset
    > of <stdint.h> for any compiler. Interestingly enough, the
    > (complete, not subset) <stdint.h> that comes with ARM's ADS
    > compiles and works perfectly with Visual C++ 6.


    Actually I would expect that to be possible with any system where
    things are built on 1, 2, 4, etc. octet sized objects. I think
    the availability is customized by simply omitting the appropriate
    definitions from stdint.h

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jan 15, 2004
    #12
  13. ark wrote:

    > I believe that a 16-bit DSP with a 16-bit byte


    What do you mean by byte?
    Did you mean to say "machine word" or "data path"?
    Or did you really mean 16-bit characters?

    > would have sizeof(int)==sizeof(short) (and ==1).


    Take a look at
    The Vector, Signal and Image Processing Library (VSIPL):

    http://www.vsipl.org/

    It defines types that are supposed to be portable
    to a wide variety of DSP target platforms.
     
    E. Robert Tisdale, Jan 15, 2004
    #13
  14. ark

    Dan Pop Guest

    In <WWhNb.53056$5V2.66126@attbi_s53> "ark" <> writes:

    >Risking to invoke flames from one Tom St Denis of Ottawa :)
    >
    >Is there any guarantee that, say,
    > sizeof(int) == sizeof(unsigned int)


    Explicitly guaranteed.

    > sizeof(long) > sizeof(char) ?


    Implicitly guaranteed for hosted implementations, because the library
    specification relies on INT_MAX >= UCHAR_MAX and this would be impossible
    if sizeof(int) == 1. Since LONG_MAX cannot be lower than INT_MAX,
    sizeof(long) cannot be 1, either, on a hosted implementation.

    Freestanding implementations with sizeof(long) == 1 do exist.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 15, 2004
    #14
  15. ark

    pete Guest

    E. Robert Tisdale wrote:
    >
    > ark wrote:
    >
    > > I believe that a 16-bit DSP with a 16-bit byte

    >
    > What do you mean by byte?
    > Did you mean to say "machine word" or "data path"?
    > Or did you really mean 16-bit characters?
    >
    > > would have sizeof(int)==sizeof(short) (and ==1).


    Since sizeof(int) is equaling 1,
    he obviously meant exactly what he said.

    Whatever else "byte" may mean in general programming,
    "byte" has a specific definition in C,
    and that is the way that that word is used on this newsgroup.

    --
    pete
     
    pete, Jan 15, 2004
    #15
  16. ark

    pete Guest

    Dan Pop wrote:
    >


    > > sizeof(long) > sizeof(char) ?

    >
    > Implicitly guaranteed for hosted implementations, because the library
    > specification relies on INT_MAX >= UCHAR_MAX and this would be
    > impossible if sizeof(int) == 1.


    I don't recall that ever being stated so plainly
    on this newsgroup before.

    --
    pete
     
    pete, Jan 15, 2004
    #16
  17. ark

    Alex Guest

    "Dan Pop" <> wrote in message
    news:bu63eq$mtp$...
    > > sizeof(long) > sizeof(char) ?

    >
    > Implicitly guaranteed for hosted implementations, because the
    > library specification relies on INT_MAX >= UCHAR_MAX [...]


    By which I presume you mean an 'int' must be able to hold all possible
    values of an 'unsigned char', required in (for example) getchar()?

    Alex
     
    Alex, Jan 15, 2004
    #17
  18. On Thu, 15 Jan 2004, pete wrote:
    >
    > Dan Pop wrote:
    > >
    > > > sizeof(long) > sizeof(char) ?

    > >
    > > Implicitly guaranteed for hosted implementations, because the library
    > > specification relies on INT_MAX >= UCHAR_MAX and this would be
    > > impossible if sizeof(int) == 1.

    >
    > I don't recall that ever being stated so plainly
    > on this newsgroup before.


    Nor do I. And even though I at first thought it was technically
    wrong because of padding bits, I now think that while it still may be
    wrong, it's less wrong than I thought.

    a) Plain char is unsigned. INT_MAX must be at least UCHAR_MAX so that
    getchar() can return any plain char value, and INT_MIN must be less than
    or equal to -32767. So the total number of values of 'int' must be at
    least UCHAR_MAX+32768, which requires more bits than CHAR_BIT. Q.E.D.

    b) Plain char is signed. The range of char, i.e., of signed char, must
    be a subrange of the range of int. But is it possible we might have

    #define CHAR_BIT 16
    #define UCHAR_MAX 65535
    #define SCHAR_MIN -32767 /* !!! */
    #define SCHAR_MAX 32767
    #define INT_MIN -32768
    #define INT_MAX 32767
    #define EOF -32768

    Is anything wrong, from the C standpoint, with these definitions?

    -Arthur
     
    Arthur J. O'Dwyer, Jan 15, 2004
    #18
  19. ark

    nrk Guest

    Arthur J. O'Dwyer wrote:

    >
    > On Thu, 15 Jan 2004, pete wrote:
    >>
    >> Dan Pop wrote:
    >> >
    >> > > sizeof(long) > sizeof(char) ?
    >> >
    >> > Implicitly guaranteed for hosted implementations, because the library
    >> > specification relies on INT_MAX >= UCHAR_MAX and this would be
    >> > impossible if sizeof(int) == 1.

    >>
    >> I don't recall that ever being stated so plainly
    >> on this newsgroup before.

    >
    > Nor do I. And even though I at first thought it was technically
    > wrong because of padding bits, I now think that while it still may be
    > wrong, it's less wrong than I thought.
    >
    > a) Plain char is unsigned. INT_MAX must be at least UCHAR_MAX so that
    > getchar() can return any plain char value, and INT_MIN must be less than
    > or equal to -32767. So the total number of values of 'int' must be at
    > least UCHAR_MAX+32768, which requires more bits than CHAR_BIT. Q.E.D.
    >
    > b) Plain char is signed. The range of char, i.e., of signed char, must
    > be a subrange of the range of int. But is it possible we might have
    >
    > #define CHAR_BIT 16
    > #define UCHAR_MAX 65535
    > #define SCHAR_MIN -32767 /* !!! */
    > #define SCHAR_MAX 32767
    > #define INT_MIN -32768
    > #define INT_MAX 32767
    > #define EOF -32768
    >
    > Is anything wrong, from the C standpoint, with these definitions?
    >


    Yes, something is wrong. If CHAR_BIT is 16, SCHAR_MIN *has* to be -32768.
    This follows from the specification that states that value bits in signed
    types have the same meaning as corresponding value bits in the unsigned
    types and the stipulation that an unsigned integer type with n bits must be
    able to represent values in the range [0, 2^(n-1)].

    -nrk.

    > -Arthur


    --
    Remove devnull for email
     
    nrk, Jan 15, 2004
    #19
  20. ark

    nrk Guest

    nrk wrote:

    <snip>

    > be able to represent values in the range [0, 2^(n-1)].


    Crap. That should read [0, 2^n - 1] of course.

    -nrk.

    <snip>

    --
    Remove devnull for email
     
    nrk, Jan 15, 2004
    #20
    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. Shailesh
    Replies:
    4
    Views:
    541
    Shailesh
    Apr 4, 2004
  2. SasQ

    integral types

    SasQ, Mar 24, 2007, in forum: C++
    Replies:
    13
    Views:
    631
    James Kanze
    Mar 30, 2007
  3. Army1987
    Replies:
    6
    Views:
    335
    CBFalconer
    Jul 7, 2007
  4. Ioannis Vranos
    Replies:
    13
    Views:
    493
    James Kanze
    Mar 10, 2008
  5. Ioannis Vranos

    Minimum sizes of integral and floating point types

    Ioannis Vranos, Mar 8, 2008, in forum: C Programming
    Replies:
    9
    Views:
    342
    Richard Heathfield
    Mar 9, 2008
Loading...

Share This Page