117

If it exists, is there header file to include?

This code results in a compilation error:

int main() {
    byte b = 2; // error
}
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
user2972135
  • 1,361
  • 2
  • 8
  • 10
  • 17
    It's called `char`. – Avidan Borisov Nov 16 '13 at 22:25
  • 1
    Indeed. `char` isn't necessarily one byte, but it's almost certainly what you're looking for. There's also `int8_t` if you particularly care about the number of bits. – Sneftel Nov 16 '13 at 22:26
  • 14
    @Ben `char` *is* necessarily one byte. It's just that a byte isn't necessarily 8 bits. – Avidan Borisov Nov 16 '13 at 22:30
  • 2
    "byte" is a term that has had a pretty rigorous definition (8 bits) since it became necessary to distinguish between processors which were 8-bit and processors which were not. There is no language or framework I know of, anywhere, which treats "byte" as anything other than 8 bits. – Sneftel Nov 16 '13 at 22:31
  • 1
    What I think you mean is that "char" is the minimal addressable unit, which is definitely true. – Sneftel Nov 16 '13 at 22:32
  • 4
    @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. – Ed S. Nov 16 '13 at 22:35
  • Granted. And it's true that at times "byte" has been used to refer to the minimal addressable unit even when that's not 8 bits. Not to put to fine a point on it, though, this is why it's called "CHAR_BIT" instead of "BYTE_BIT". The entire benefit of having "char" and "byte" be different words is to allow one to discuss either their machine's particular width, or a standard(-ish)-length element. – Sneftel Nov 16 '13 at 22:41
  • 14
    @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. – Keith Thompson Nov 17 '13 at 01: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)? – einpoklum Dec 21 '15 at 10:56

9 Answers9

113

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.

dave_k_smith
  • 655
  • 1
  • 7
  • 22
jwodder
  • 54,758
  • 12
  • 108
  • 124
  • 10
    Not quite. `char`, `signed char`, and `unsigned char` are three distinct types. `char` has the same representation as one of the other two. – Keith Thompson Nov 17 '13 at 01:05
  • 3
    If `unsigned char` is bigger than 8 bits, then `uint8_t` will not be defined. – Keith Thompson Nov 17 '13 at 01:06
  • 2
    if you're in objective-c, you may need to include `` instead of ``. – orion elenzil Mar 07 '17 at 00:43
  • 4
    @orionelenzil The question was about C++, not objective C. – Pharap Aug 02 '17 at 10:23
  • 4
    @pharap - one writing C or C++ in an Objective-C context may find my comment useful. – orion elenzil Aug 02 '17 at 17:15
  • @orionelenzil Would it not be better to make a new question specifically for C or Objective C from which people can find the answer. The three are different languages after all. – Pharap Aug 02 '17 at 17:41
  • @pharap - well, i don't think so. ordinary C and C++ language issues come up while writing Objective-C. The question/answer would be basically identical to what's here, except with the minor change i've noted. – orion elenzil Aug 07 '17 at 22:06
  • 13
    Please update this answer to reflect the existence of `std::byte` in C++17. – rdb Apr 02 '18 at 18:58
81

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

C++ 17 introduced it.

maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
  • 4
    `std::byte` cannot have arithmetic performed on it, which might be a deal breaker. – Pharap Aug 02 '17 at 10:22
  • 4
    @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`). – maxschlepzig Aug 02 '17 at 19:53
  • @Pharap - and that's exaclty what is good about it. "byte" is about store some data, not about interpret it as some number – Ezh Dec 09 '21 at 12:08
  • @Ezh I never said it was a bad thing, just that it might be a deal breaker (i.e. for some use cases). The answer doesn't mention this, and I think it's a fairly important thing to be aware of when one is trying to choose the right tool for the job. I.e. my comment was adding information that was not included in the answer itself. – Pharap Dec 10 '21 at 10:39
  • 1
    @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 Dec 11 '21 at 14:48
  • 1
    @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_. – Pharap Dec 11 '21 at 20:05
  • "there's more than one definition of 'byte' " No, this is about programming, specifically about C++ - your subjective interpretations of what a "byte" represents in another context are irrelevant. A `char` is defined as having a size of 1 byte - and the size of a byte is defined by `CHAR_BIT` - which in practice must be at least 7 bit up to C++11 to be able to represent the specified values, and at least 8 bits since then. – ABaumstumpf Feb 03 '23 at 19:19
40

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.

Darren Gansberg
  • 766
  • 7
  • 13
  • 67
    How is `bitset<8>` better than `unsigned char`? – Keith Thompson Nov 17 '13 at 03:15
  • 17
    Its not. use unsigned char – Yasser Asmi Feb 24 '17 at 06:27
  • 4
    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. – Mark Galeck Apr 28 '17 at 19:07
  • 18
    You might want to update this answer as there is now a `std::byte` – NathanOliver Mar 27 '18 at 13:28
  • @MarkGaleck *You are technically correct. The best kind of correct.* The fuddy-duddies here frown on memes but Search for it if you're not familiar. Though you mean `CHAR_BIT'. I'm this close to editing the answer but the OP of the answer appears to be active. I'd mention the answer is for versions before C++17 and the nearly 50 years late `std::byte` given C with possible hindsight `sizeof(char)==1` should never have been. https://en.cppreference.com/w/cpp/types/climits – Persixty May 14 '20 at 00:51
  • 1
    @Persixty oops why did I say `BIT_CHAR` . It's a mystery. A mystery of the universe.. – Mark Galeck May 15 '20 at 02:32
31

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

typedef unsigned char BYTE;
rmp
  • 1,053
  • 7
  • 15
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).

m8mble
  • 1,513
  • 1
  • 22
  • 30
  • 5
    Why is this enum better, in your opinion, than `typedef unsigned char byte;` or `typedef std::uint8_t byte;` ? – einpoklum Apr 27 '17 at 14:53
  • 3
    @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](https://stackoverflow.com/q/43551665/427158). When you want properly alias some bytes you need `char*`, `unsigned char*` or `std::byte*`. – maxschlepzig Jun 15 '17 at 21:29
  • 1
    Except `std::byte` cannot have arithmetic performed on it, which might be a deal breaker. – Pharap Aug 02 '17 at 10:21
3

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

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54
  • `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. – Jan Schultke Aug 28 '23 at 17:13
0

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

ruipacheco
  • 15,025
  • 19
  • 82
  • 138
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.

Chris Reid
  • 460
  • 4
  • 9
0

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
Aylian Craspa
  • 422
  • 5
  • 11