468,512 Members | 1,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

C++ byte

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.
Apr 1 '08 #1
7 11405
lazy wrote:
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
Apr 1 '08 #2
On 1 Apr, 08:24, Kai-Uwe Bux <jkherci...@gmx.netwrote:
lazy wrote:
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.
It is, however, guaranteed to have an object (and value)
representation of exactly one byte.

DP
Apr 1 '08 #3
On Mar 31, 11:41 pm, Triple-DES <DenPlettf...@gmail.comwrote:
On 1 Apr, 08:24, Kai-Uwe Bux <jkherci...@gmx.netwrote:
lazy wrote:
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.

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

DP

Thanks.
Apr 1 '08 #4
On Apr 1, 8:24 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
lazy wrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 1 '08 #5
On 2008-04-01 14:58, James Kanze wrote:
On Apr 1, 8:24 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>lazy wrote:
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.

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).

--
Erik Wikström
Apr 1 '08 #6
On Tue, 01 Apr 2008 18:30:19 GMT, Erik Wikström
<Er***********@telia.comwrote in comp.lang.c++:
On 2008-04-01 14:58, James Kanze wrote:
On Apr 1, 8:24 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
lazy wrote:
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.
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).
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 <climitswith 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
Apr 2 '08 #7
On Apr 1, 8:30 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-04-01 14:58, James Kanze wrote:
On Apr 1, 8:24 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
lazy wrote:
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.
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).
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 2 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Cook | last post: by
8 posts views Thread by intrepid_dw | last post: by
6 posts views Thread by Dennis | last post: by
4 posts views Thread by Frederick Gotham | last post: by
10 posts views Thread by Scott Townsend | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.