C++ byte

L

lazy

Is there a datatype for byte?
I want to write an API that takes in an array of bytes.
I can use char array, but not sure if sizeof(char) will always be 1
now or in future on all machines(32/64bit) and compilers.
Is sizeof(char) always gauranteed to be one?

Thanks.
 
K

Kai-Uwe Bux

lazy said:
Is there a datatype for byte?
I want to write an API that takes in an array of bytes.
I can use char array, but not sure if sizeof(char) will always be 1
now or in future on all machines(32/64bit) and compilers.
Is sizeof(char) always gauranteed to be one?

sizeof(char) is guaranteed to be one.

However, a char is not guaranteed to be an octet of bits.


Best

Kai-Uwe Bux
 
T

Triple-DES

sizeof(char) is guaranteed to be one.

However, a char is not guaranteed to be an octet of bits.

It is, however, guaranteed to have an object (and value)
representation of exactly one byte.

DP
 
E

Erik Wikström

Neither is a byte. I've seen bytes of 6, 7 and 9 bits as well,
and on a PDP-10, the size of a byte was programmable.

I think, however, that the minimal size of a char in C++ is 7 bits since
the standard identifies at least 99 characters in the basic character
set which must be encodable in a char (sections 2.2 and 3.9.1 in the
standard).
 
R

Ron Natalie

lazy said:
Is there a datatype for byte?
I want to write an API that takes in an array of bytes.
I can use char array, but not sure if sizeof(char) will always be 1
now or in future on all machines(32/64bit) and compilers.
Is sizeof(char) always gauranteed to be one?
One of the farking major defects in C and C++ is that
char does double duty. It is both the "byte", i.e., the smallest
addressable unit of storage and the native character site.
You can't even use blasted wchar_t to implement a real
native character type separate from byte because most
of the library interfaces don't have wchar_t analogs.
 
J

Jack Klein

I think, however, that the minimal size of a char in C++ is 7 bits since
the standard identifies at least 99 characters in the basic character
set which must be encodable in a char (sections 2.2 and 3.9.1 in the
standard).

No, the minimum size of a char in C++ is 8 bits. Unfortunately, you
can't gather that directly from the C standard.

You can gather that from 18.2.1.2 (see footnotes 181 and 182 in C++
98, might have different numbers in later versions).

Then you go to 18.2.2 where the C++ incorporates <climits> with the
statement "The contents are the same as the Standard C library header
<limits.h>."

When the C++ standard incorporates a C header, any C95 requirements
apply in C++, unless specifically excluded by text in the C++
standard. In this case, there are no such exclusions, so one can look
up a copy of the C95 standard and find out that C++ requires a minimum
value of 8 for CHAR_BIT.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

James Kanze

On 2008-04-01 14:58, James Kanze wrote:
I think, however, that the minimal size of a char in C++ is 7
bits since the standard identifies at least 99 characters in
the basic character set which must be encodable in a char
(sections 2.2 and 3.9.1 in the standard).

The C and C++ standards use a non-standard definition of byte.
They require a byte to have at least 8 bits (since the smallest
legal value of UCHAR_MAX is 255); they also speak of bytes
regardless of the implementation, where as of course, many early
machines didn't have bytes at all. (Thus, for example, when a
DSP defines CHAR_BIT as 32, and sizeof( int ) == sizeof( char ),
the C/C++ standard speaks of 32 bit bytes, when according to the
traditional definition, there are no bytes, just words.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,700
Members
45,491
Latest member
JennyPerso

Latest Threads

Top