If it exists, is there header file to include?
This code results in a compilation error:
int main() {
byte b = 2; // error
}
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.
char, signed char, and unsigned char are three distinct types. char has the same representation as one of the other two.unsigned char is bigger than 8 bits, then uint8_t will not be defined.std::byte in C++17.std::byte cannot have arithmetic performed on it, which might be a deal breaker.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).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.
bitset<8> better than unsigned char?std::byteunsigned 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.if you are using windows, in WinDef.h you have:
typedef unsigned char BYTE;
WTypesBase.h, it is a typedef to byte.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).
typedef unsigned char byte; or typedef std::uint8_t byte; ?char*, unsigned char* or std::byte*.std::byte cannot have arithmetic performed on it, which might be a deal breaker.No, but since C++11 there is [u]int8_t.
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.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.
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
The first thing that came to mind:
typedef char byte;
char.charis necessarily one byte. It's just that a byte isn't necessarily 8 bits.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.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.using byte = unsigned charand be done with it (like rmp's answer suggests)?