# size of a byte and int?

Started by January 25, 2005
```Hi All,

Is that true that size of a byte not necessarily 8-bit?
What the std. says? If that true, then what will the size of an int,
i mean what sizeof(int) should return?

On my machine sizeof(char) == sizeof(int).
TMS320C5402 DSP (with 16-bit word size).
both returns one. So, it holds true.

But my interpretation is :
Size of int,float etc is specified in terms of bytes, not bits...which is a
standard i.e int -> 2 bytes, char -> 1 byte etc... now, actual size of these
depends up on no. of bits in a byte... which is implementation defined. So,
if we say on a machine its defined 1 byte = 8 bits then size(char) = 1byte =
8 bits size(int) = 2 bytes = 16 bits...
but on other machine 1 byte = 16 bits the size(char) = 1byte = 16 bits and
Size(Int) =2 bytes= 32 bits. In no case, it can be same for both char & int.

-Neo

```
```Neo wrote:
>
> Hi All,
>
> Is that true that size of a byte not necessarily 8-bit?

Width is measured in bits.
Size is measured in bytes.

--
pete
```
```"pete" <pfiland@mindspring.com> wrote in message
news:41F5FC17.1161@mindspring.com...
> Neo wrote:
>>
>> Hi All,
>>
>> Is that true that size of a byte not necessarily 8-bit?
>
> Width is measured in bits.
> Size is measured in bytes.
>
> --
> pete

O'kay! What does int and char (data types in C) are measured in?
-Neo

```
```Neo wrote:
>
> Is that true that size of a byte not necessarily 8-bit?

Yes.

> What the std. says?

It says that a byte is CHAR_BIT bits in width, where CHAR_BIT >= 8.

> If that true, then what will the size of an int,
> i mean what sizeof(int) should return?

It will be whatever it is on a given implementation.

> On my machine sizeof(char) == sizeof(int).
> TMS320C5402 DSP (with 16-bit word size).
> both returns one. So, it holds true.
>
> But my interpretation is :
> Size of int,float etc is specified in terms of bytes, not bits...

No. Integers and floats are specified in terms of value limits
and precision. The minimum range that a signed int must be able
to represent is -32767..32767. Mathematics dictate that this
requires at least 16 bits (including a sign bit.) Thus, an
int will require _at least_ as many bytes as is required to
store 16-bits.

Google for N869 and read the last public draft of the C99 standard.
--
Peter

```
```Neo wrote:
>
> "pete" <pfiland@mindspring.com> wrote in message
> news:41F5FC17.1161@mindspring.com...
> > Neo wrote:
> >>
> >> Hi All,
> >>
> >> Is that true that size of a byte not necessarily 8-bit?
> >
> > Width is measured in bits.
> > Size is measured in bytes.
> >
> > --
> > pete
>
> O'kay! What does int and char (data types in C) are measured in?

Bytes or bits, depending on whether you want size or width.

sizeof(char) is 1, by definition.

A char comprises CHAR_BIT bits. CHAR_BIT is defined in <limits.h>
and its value can vary from one implementation to the next, but
it can be no lower than 8.

sizeof(int) is at least 16 bits wide. Therefore, it must be
at least (16 + CHAR_BIT - 1) / CHAR_BIT bytes in size (ignoring
any remainder).

int must be able to represent all values in the range -32767 to +32767.
```
```>Bytes or bits, depending on whether you want size or width.
>
>sizeof(char) is 1, by definition.

Could be 2 on some DSPs (IIRC TI's).

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use <same-name>@monlynx.de instead !
```
```On Tue, 25 Jan 2005 13:11:04 +0530, "Neo"
<timeless_illusion@yahoo.com> wrote:

>Hi All,
>
>Is that true that size of a byte not necessarily 8-bit?

I think common sense is that a byte is nowadays 8 bit.

>What the std. says? If that true, then what will the size of an int,

Don't mix byte with char ! I don't think there is a std defining the
width of a byte.

>i mean what sizeof(int) should return?
>
>On my machine sizeof(char) == sizeof(int).
>TMS320C5402 DSP (with 16-bit word size).
^^^
That's it, they speak of words avoiding the term byte.

A reason, to define __u8,__u16,__u32 etc. (or the like) depending on
the cpu and/or compiler.
--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use <same-name>@monlynx.de instead !
```
```42Bastian Schick wrote:
>>Bytes or bits, depending on whether you want size or width.
>>
>>sizeof(char) is 1, by definition.
>
>
> Could be 2 on some DSPs (IIRC TI's).

No, it can't. By _definition_, sizeof(chat) is 1. This is stated in the
C standard.

--
Paul Black                        mailto:paul.black@oxsemi.com
Oxford Semiconductor Ltd          http://www.oxsemi.com
25 Milton Park, Abingdon,         Tel: +44 (0) 1235 824 909
Oxfordshire.    OX14 4SH          Fax: +44 (0) 1235 821 141
```
```42Bastian Schick wrote:
> "Neo"
> > Hi All,
> >
> > Is that true that size of a byte not necessarily 8-bit?
>
> I think common sense is that a byte is nowadays 8 bit.

Sense, common to what? An 8-bit entity is called an 'octet'
explicitly by many standards and protocols, precisely to avoid
confusion.

> > What the std. says? If that true, then what will the size of
> > an int,
>
> Don't mix byte with char ! I don't think there is a std
> defining the width of a byte.

The standard does define that a byte has an implementation width
which is big enough to hold the representation of a character
from the basic character set. The standard also states that the
size of all three character types is 1 byte.

> >i mean what sizeof(int) should return?
> >
> >On my machine sizeof(char) == sizeof(int).
> >TMS320C5402 DSP (with 16-bit word size).
>                                ^^^
> That's it, they speak of words avoiding the term byte.

Who is 'they'? A C programmer will still speak of bytes.

> A reason, to define __u8,__u16,__u32 etc. (or the like)
> depending on the cpu and/or compiler.

Programs targetting hosted implementations will generally
have little need for such types. Indeed, implementations
on certain architectures may not be able to represent such
precise width types, outside of inefficient emulation.

C99 introduced the intN_t types to cater for programs which
do rely on precise width twos complement integer types,
however programs which make use of them are not strictly
conforming.

--
Peter

```
```"Peter Nilsson" <airia@acay.com.au> wrote in message
> 42Bastian Schick wrote:
[-snip-]
> C99 introduced the intN_t types to cater for programs which
> do rely on precise width twos complement integer types,
> however programs which make use of them are not strictly
> conforming.
>
> --
> Peter
>
Why not conforming? Then why does the std. defines these?
-Neo

```