byte/char

This is a discussion on byte/char within the C++ Programming forums, part of the General Programming Boards category; Is BYTE/char the same, right? BYTE is actually the typedef of char?...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    byte/char

    Is BYTE/char the same, right?
    BYTE is actually the typedef of char?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    That's a good question. The size of any C++ object is a multiple of the size of a char, and a char is only guaranteed to have at least 8 bits. On the other hand, when Stroustrup refers to a byte he seems to be assuming it consists of exactly 8 bits, so I think the answer is no.
    Last edited by robatino; 01-10-2007 at 02:44 PM.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    > Is BYTE/char the same, right?

    No. Even on those cases where BYTE may be defined as a typedef for char, BYTE is not a char. I understand I'm being pedantic. But it's an important concept to keep in mind that typedefs of this nature have a very specific purpose; to provide the coder with a new type context in which to create and use their objects.

    In the presence of a statement the likes of "typedef BYTE char", BYTE is one type and char is another type. They may essentially map to the same built-in type. But they have a different meaning. I find this distinction important.

    > BYTE is actually the typedef of char?

    On some systems yes. On others no. Take a look at http://en.wikipedia.org/wiki/Byte for other potential bytes
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Of course, if you're specifically talking about the BYTE typedef of the Win32 API ... the answer is still no The WinAPI BYTE is a typedef for unsigned char.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The BYTE datatype is probably a typedef for unsigned char on your platform, but I don't think it would guaranteed to be (otherwise it wouldn't be useful).

    A byte is always the same as a char in C++, but not necessarily 8 bits.

    http://www.parashift.com/c++-faq-lit...sic-types.html

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    There is no type byte in standard C/C++. (You may have a library that typedefs it as a char.)

    In general, they are not the same. A char is at least one byte. But, it does get confusing...

    The definition of type char is clear - At least 8 bits (as required for the ASCII character set). On a 16 bit machine, it might be 16 bits. (On a 16 bit machine, every RAM location contains 16 bits.)

    The definition of a byte is not so clear. In everyday technical jargon everybody knows that a byte is always 8 bits. However, the exact technical definition is apparently not so precise!

    If I'm reading the it correctly, the language standard defines them similarly (they both can hold one character). But, they are defining the word / term byte, not a type-byte.

    From the C Language Standard:
    3.6
    1 byte- addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

    2 NOTE 1 It is possible to express the address of each individual byte of an object uniquely.
    3 NOTE 2 A byte is composed of a contiguous sequence of bits, the number of which is implementationdefined.
    The least significant bit is called the low-order bit; the most significant bit is called the high-order
    bit.
    6.2.5 Types
    1…
    2…
    3 An object declared as type char is large enough to store any member of the basic
    execution character set. If a member of the basic execution character set is stored in a
    char object, its value is guaranteed to be positive. If any other character is stored in a
    char object, the resulting value is implementation-defined but shall be within the range
    of values that can be represented in that type.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    According to the C++ FAQ, the word/term byte refers to the same number of bits that are used by the char type. In other words, if char is 9 bits, then char is one byte and a byte is 9 bits on that platform.

    Of course it seems the original question is probably about the BYTE datatype in Windows.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21