char

Discussion in 'C++' started by Chameleon, Jan 9, 2007.

  1. Chameleon

    Chameleon Guest

    char is 1 byte length in standard c/c++?

    I ask because short/int/long has not a specific length in bytes, but:
    short is smaller or equal to int
    int is smaller or equal to long
    Chameleon, Jan 9, 2007
    #1
    1. Advertising

  2. Chameleon

    Ron Natalie Guest

    Chameleon wrote:
    > char is 1 byte length in standard c/c++?


    By definition. A char is exactly one byte in size because
    that's how a byte in C++ is defined. However, it points out
    one glaring problem in C and C++. Char serves double duty:
    1. It's the native character size.
    2. It's the smallest addressable unit of storage.

    That means that you can't really use char's if you have 16 bit
    native characters unless you want to give up the ability to
    address on 8 bit boundaries. What makes this worse is that
    C++ rots big time on wchar_t support. Many important interfaces
    (filenames, program arguments) don't have any wide char interface.
    >
    > I ask because short/int/long has not a specific length in bytes, but:
    > short is smaller or equal to int
    > int is smaller or equal to long

    Note that "bytes" is not any specific number of bits in C++.

    There are practical lower limits on short and long (16 and 32 bits)
    in addition to the char <= short <= int <= long.
    Ron Natalie, Jan 9, 2007
    #2
    1. Advertising

  3. Chameleon wrote:

    > char is 1 byte length in standard c/c++?


    Yes, but the word byte has several usages depending on conext. In some
    contexts it means minimal addressable unit, in some others it means 8 bits
    (because is the same as the previous one in that context, of for other
    reasons), and in standard C++ context it means the size of a char.

    Then the size of char is 1 byte by definition, no matter if his bit size is
    8, 16, 32 or whatever.

    To avoid ambiguities is better to use the word octect to refer to an 8 bit
    size, like Internet RFC documents do.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 9, 2007
    #3
  4. Ron Natalie wrote:
    > Chameleon wrote:
    >> char is 1 byte length in standard c/c++?

    >
    > By definition. A char is exactly one byte in size because
    > that's how a byte in C++ is defined. However, it points out
    > one glaring problem in C and C++. Char serves double duty:
    > 1. It's the native character size.
    > 2. It's the smallest addressable unit of storage.
    >
    > That means that you can't really use char's if you have 16 bit
    > native characters unless you want to give up the ability to
    > address on 8 bit boundaries.


    I honestly don't understand that statement. Could you please
    elaborate?

    > What makes this worse is [..]
    >>
    >> I ask because short/int/long has not a specific length in bytes, but:
    >> short is smaller or equal to int
    >> int is smaller or equal to long

    > Note that "bytes" is not any specific number of bits in C++.


    What about 'CHAR_BIT'? Is it not specific enough?

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 9, 2007
    #4
  5. Chameleon

    Jack Klein Guest

    On Tue, 09 Jan 2007 16:20:18 -0500, Ron Natalie <>
    wrote in comp.lang.c++:

    > Chameleon wrote:
    > > char is 1 byte length in standard c/c++?

    >
    > By definition. A char is exactly one byte in size because
    > that's how a byte in C++ is defined. However, it points out
    > one glaring problem in C and C++. Char serves double duty:
    > 1. It's the native character size.
    > 2. It's the smallest addressable unit of storage.
    >
    > That means that you can't really use char's if you have 16 bit
    > native characters unless you want to give up the ability to
    > address on 8 bit boundaries. What makes this worse is that
    > C++ rots big time on wchar_t support. Many important interfaces
    > (filenames, program arguments) don't have any wide char interface.


    In C and C++ implementations where chars have 16 (or 32) bits, exist
    primarily on DSPs (Digital Signal Processors) that physically CAN'T
    access memory in units smaller than the 16 or 32 bit machine words.

    Typically they are used in embedded systems running in free-standing
    environments where there aren't any filenames or program arguments,
    anyway.

    But if there was a hosted environment for a CHAR_BIT > 8 system, I am
    quite sure that the underlying platform would find a way to deal with
    these things, if necessary.

    You'd be surprised how little difference 16 bit chars makes to code
    designed and written with a little care.

    --
    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.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 9, 2007
    #5
  6. Jack Klein wrote:

    > You'd be surprised how little difference 16 bit chars makes to code
    > designed and written with a little care.


    So you must avoid to take too much care?

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 10, 2007
    #6
  7. Chameleon

    Sumit Rajan Guest

    Sumit Rajan, Jan 10, 2007
    #7
    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. wwj
    Replies:
    7
    Views:
    546
  2. wwj
    Replies:
    24
    Views:
    2,501
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    470
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    577
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
Loading...

Share This Page