Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

char size

MatthewDMatthewD Posts: 297Member
The reference book I use says that char size is 8 bits wide, regardless of 16 or 32 bit platforms. Is this guaranteed by the standard, regardless of compiler?

When I get a char from a std::string using c_str() function, is the size of each char 8 bits? What about a wstring? -- what type and size does it hold?

Thanks,
Matthew.

Comments

  • DariusDarius Posts: 1,666Member
    : The reference book I use says that char size is 8 bits wide, regardless of 16 or 32 bit platforms. Is this guaranteed by the standard, regardless of compiler?
    :
    : When I get a char from a std::string using c_str() function, is the size of each char 8 bits? What about a wstring? -- what type and size does it hold?
    :
    : Thanks,
    : Matthew.
    :
    Yes a char is always 8 bits wide. The value you get from string::c_str() is a char *, so the data it points to is 8 bits. wchar stands for wide character or Unicode, which is to the world what ASCII is to America. Unicode characters are always 16-bit.

    "No Bad Religion song can make your life complete."
    -No Direction, Bad Religion


  • Null and VoidNull and Void Posts: 1,202Member
    No, a character isn't guaranteed to be 1 byte. It IS however guaranteed to be AT LEAST 1 byte (see page 23 of the C99 Draft for proof). However, I've never seen "char" be anything other than 1 byte.


  • Eric TetzEric Tetz Posts: 2,141Member
    : The reference book I use says that char size is 8 bits wide, regardless of 16 or 32 bit platforms. Is this guaranteed by the standard, regardless of compiler?

    It's guaranteed to be at [italic]least[/italic] 8 bits, but it could be more. There are machines where char is 9 or 11 bits, or even 32 or 64 bits.

    : When I get a char from a std::string using c_str() function, is the size of each char 8 bits?

    There are CHAR_BIT bits in each char. CHAR_BIT is defined in .

    :What about a wstring? -- what type and size does it hold?

    It's required to be able to hold at least as much as a regular char. Beyond that, I believe it is implementation defined (but I'm too lazy to look it up to be sure).

    Cheers,
    Eric


  • VincentVincent Posts: 54Member
    : : The reference book I use says that char size is 8 bits wide, regardless of 16 or 32 bit platforms. Is this guaranteed by the standard, regardless of compiler?
    :
    : It's guaranteed to be at [italic]least[/italic] 8 bits, but it could be more. There are machines where char is 9 or 11 bits, or even 32 or 64 bits.
    :
    : : When I get a char from a std::string using c_str() function, is the size of each char 8 bits?
    :
    : There are CHAR_BIT bits in each char. CHAR_BIT is defined in .
    :
    : :What about a wstring? -- what type and size does it hold?
    :
    : It's required to be able to hold at least as much as a regular char. Beyond that, I believe it is implementation defined (but I'm too lazy to look it up to be sure).
    :
    : Cheers,
    : Eric
    :
    :
    Hi,

    A char has a size of 1 byte, but if you want to preven't matters with the size of all type of data use "sizeof" function. In this case you won't have any matters with the length of int, double and other types which depends on your machine (little endian and big endian types).

    Bye,

    Vincent.

  • CytCyt Posts: 557Member
    : No, a character isn't guaranteed to be 1 byte. It IS however guaranteed to be AT LEAST 1 byte (see page 23 of the C99 Draft for proof). However, I've never seen "char" be anything other than 1 byte.
    :

    I have. The c compiler for many Texas Instruments DSPs is a 16 bit compiler, having all common types set to 16 bit (char, int, and pointers). It does happen, but is very seldom.

    Apart from that, the 16 bit chars normally only have their 8 LSBs used.


  • blitzblitz Posts: 620Member
    : :What about a wstring? -- what type and size does it hold?
    :
    : It's required to be able to hold at least as much as a regular char. Beyond that, I believe it is implementation defined (but I'm too lazy to look it up to be sure).
    :

    this is from the C++ standard draft (dec. 1996) - it should be pretty close to the actual standard ;-)

    "Type wchar_t is a distinct type whose values can represent
    distinct codes for all members of the largest extended character
    set specified among the supported locales (_lib.locale_).
    Type wchar_t shall have the same size, signedness, and alignment
    requirements (_intro.memory_)as one of the other integral types,
    called its underlying type."



Sign In or Register to comment.