131

If it exists, is there header file to include?

This code results in a compilation error:

int main() {
    byte b = 2; // error
}
10
  • 19
    It's called char. Commented Nov 16, 2013 at 22:25
  • 14
    @Ben char is necessarily one byte. It's just that a byte isn't necessarily 8 bits. Commented Nov 16, 2013 at 22:30
  • 5
    @Ben: You're apparently not familiar with the more exotic platforms in existence. A byte is certainly not defined to be 8 bits, regardless of the fact that 8 bit bytes are predominant. That's why we have CHAR_BIT. I have worked on more than one embedded system where bytes are not 8 bits in length. A char is defined to have a size of 1, so yes, a char is always a byte. Commented Nov 16, 2013 at 22:35
  • 15
    @Ben: The C and C++ standards unambiguously define a "byte" as the size of a char, which is at least 8 bits. The term "byte" may be defined differently in other contexts, but when discussing C or C++ it's better to stick to the standard's definition. Commented Nov 17, 2013 at 1:03
  • 5
    OP, I'd reconsider your accepted answer. Really. Also, If a char is guaranteed to have size 1, why note write using byte = unsigned char and be done with it (like rmp's answer suggests)? Commented Dec 21, 2015 at 10:56

10 Answers 10

117

No, there is no type called "byte" in C++. What you want instead is unsigned char (or, if you need exactly 8 bits, uint8_t from <cstdint>, since C++11). Note that char is not necessarily an accurate alternative, as it means signed char on some compilers and unsigned char on others.

Sign up to request clarification or add additional context in comments.

8 Comments

Not quite. char, signed char, and unsigned char are three distinct types. char has the same representation as one of the other two.
If unsigned char is bigger than 8 bits, then uint8_t will not be defined.
@orionelenzil The question was about C++, not objective C.
@pharap - one writing C or C++ in an Objective-C context may find my comment useful.
Please update this answer to reflect the existence of std::byte in C++17.
|
100

Yes, there is std::byte (defined in <cstddef>).

C++ 17 introduced it.

8 Comments

std::byte cannot have arithmetic performed on it, which might be a deal breaker.
@Pharap, it depends - not being able to accidentally do arithmetic may be seen as advantage for some use cases. Since std::byte is just an addition one can choose the right tool for the job (i.e. either std::byte, char, unsigned char or uint_8).
@Pharap - and that's exaclty what is good about it. "byte" is about store some data, not about interpret it as some number
@Pharap neither original question, nor this answer, nor definition of "byte" is related to the ability of performing arithmetic operation on it. What you say is true, it is just absolutely not related. It sounds like "-Do we have a string? -Yes, it is std::string, just remember - it doesn't support arithmetic operations"
@Ezh And yet many languages that support a 'byte' data type also support performing arithmetic on that type (e.g. Java, C#, Kotlin, Go). The strict, pedantic definition of a byte may well say nothing about performing arithmetic, but the strict definition is not necessarily the same as people's expectations. (Or to put it another way, there's more than one definition of 'byte'.) The strict definition of a character also says nothing about performing arithmetic on it or characters having an ordering, and yet many programmers will expect these features because many languages support them.
|
39

No there is no byte data type in C++. However you could always include the bitset header from the standard library and create a typedef for byte:

typedef bitset<8> BYTE;

NB: Given that WinDef.h defines BYTE for windows code, you may want to use something other than BYTE if your intending to target Windows.

Edit: In response to the suggestion that the answer is wrong. The answer is not wrong. The question was "Is there a 'byte' data type in C++?". The answer was and is: "No there is no byte data type in C++" as answered.

With regards to the suggested possible alternative for which it was asked why is the suggested alternative better?

According to my copy of the C++ standard, at the time:

"Objects declared as characters (char) shall be large enough to store any member of the implementations basic character set": 3.9.1.1

I read that to suggest that if a compiler implementation requires 16 bits to store a member of the basic character set then the size of a char would be 16 bits. That today's compilers tend to use 8 bits for a char is one thing, but as far as I can tell there is certainly no guarantee that it will be 8 bits.

On the other hand, "the class template bitset<N> describes an object that can store a sequence consisting of a fixed number of bits, N." : 20.5.1. In otherwords by specifying 8 as the template parameter I end up with an object that can store a sequence consisting of 8 bits.

Whether or not the alternative is better to char, in the context of the program being written, therefore depends, as far as I understand, although I may be wrong, upon your compiler and your requirements at the time. It was therefore upto the individual writing the code, as far as I'm concerned, to do determine whether the suggested alternative was appropriate for their requirements/wants/needs.

6 Comments

How is bitset<8> better than unsigned char?
Its not. use unsigned char
The answer is wrong, and all the other ones are wrong, except jwodder which is the only correct answer. byte is BIT_CHAR bits, not 8 bits.
You might want to update this answer as there is now a std::byte
FWIW, this could be helpful for constructing a platform-independent binary data representation (e.g., for a custom cross-platform (de)serializer). After all, OP didn't define a "byte", and it obviously has different definitions on different platforms. But maybe OP wants to represent a platform-A byte on platform B. unsigned char cannot do that for you. bitset<N> can (and is perhaps the best option for that use case, depending on whether uint8_t would work for you). This is pedantic, but so are many of the other answers and comments here.
|
32

if you are using windows, in WinDef.h you have:

typedef unsigned char BYTE;

1 Comment

And, in WTypesBase.h, it is a typedef to byte.
24

Using C++11 there is a nice version for a manually defined byte type:

enum class byte : std::uint8_t {};

That's at least what the GSL does.

Starting with C++17 (almost) this very version is defined in the standard as std::byte (thanks Neil Macintosh for both).

3 Comments

Why is this enum better, in your opinion, than typedef unsigned char byte; or typedef std::uint8_t byte; ?
@einpoklum, it increases type safety. For example, you get a compile error when you accidentally multiply such a byte value. Although it doesn't help with aliasing. When you want properly alias some bytes you need char*, unsigned char* or std::byte*.
Except std::byte cannot have arithmetic performed on it, which might be a deal breaker.
3

No, but since C++11 there is [u]int8_t.

1 Comment

uint8_t is not a byte-like type, and doesn't have the same exemptions from strict aliasing as unsigned char or std::byte. It shouldn't be mistaken for a byte type; it's a type for performing 8-bit arithmetic; nothing more.
0

There's also byte_lite, compatible with C++98, C++11 and later.

Comments

0
namespace std
{
  // define std::byte
  enum class byte : unsigned char {};

};

This if your C++ version does not have std::byte will define a byte type in namespace std. Normally you don't want to add things to std, but in this case it is a standard thing that is missing.

std::byte from the STL does much more operations.

Comments

-1

in c++ char is 8 bit in length but the value differs depending on the use so if you need it to be exact value you have to use its prefixed versions such as

unsigned 0 - 255
signed (-127) - 127 (with a sign bit in front)

if you only use char it will auto select type of variable according to the value we insert and sometime it can be give unpredictable results.

there is also some other types for handling unicodes as chars

char16_t
char32_t
wchar_t

Comments

-1

The first thing that came to mind:

typedef char byte;

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.