size of a byte and int?

N

Neo

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.

Please help me where I am missing.

-Neo
 
P

Peter Nilsson

Neo said:
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.
Please help me where I am missing.

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

infobahn

Neo said:
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.
 
4

42Bastian Schick

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

Peter Nilsson

42Bastian said:
"Neo"

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.
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.
^^^
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.
 
N

Neo

Peter Nilsson said:
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.
Why not conforming? Then why does the std. defines these?
-Neo
 
T

Thomas Stegen

Neo said:
>

Why not conforming?

(They are not _strictly_ conforming)

Because a platform is not required to provide them.
Just in case it cannot support them.
Then why does the std. defines these?

Probably to make the interface to this functionality
uniform across the subset of platforms that can support
them. This is not a bad thing since code that really
needs this is unlikely to ever have much use on a
platform which cannot provide this.
 
D

David

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

No, sizeof(char) is 1 even on DSP's that cannot access smaller than 16-bit
data. In the case of the TMS320F24x, sizeof(char) = sizeof(int), with
both being 16-bit. Makes the chip a real pain.
 
H

Hans-Bernhard Broeker

[F'up2 cut down --- should have been done by OP!]

In comp.arch.embedded Neo said:
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?

You cross-posted this question to two newsgroups, one of which (c.a.e)
it is seriously off-topic in. Please don't do that, or if you do, at
least explain why, and set a Followup-To. As is, your posting
silently assumes a context that only applies to half your audience,
causing all kinds of needless confusion.

In the context of comp.arch.embedded, your question doesn't make much
sense at all. In the context of comp.lang.c, the answers to the above
are: Yes. The same. Implementation-defined. (In that order).

Your confusion seems to come from the fact that you don't realize that
sizeof(int), too, is implementation-defined (within limitations set up
by the standard on the range of values an int must at least be able to
hold). In theory, an implementation could have, say

CHAR_BITS == 13
sizeof(short) == 2
sizeof(int) == 3
sizeof(long) == 5
sizeof(float) == 7
sizeof(double) == 9

just for the perverse fun of it.
 
N

Neo

Hans-Bernhard Broeker said:
[F'up2 cut down --- should have been done by OP!]

In comp.arch.embedded Neo said:
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?

You cross-posted this question to two newsgroups, one of which (c.a.e)
it is seriously off-topic in. Please don't do that, or if you do, at
least explain why, and set a Followup-To. As is, your posting
silently assumes a context that only applies to half your audience,
causing all kinds of needless confusion.

In the context of comp.arch.embedded, your question doesn't make much
sense at all. In the context of comp.lang.c, the answers to the above
are: Yes. The same. Implementation-defined. (In that order).

Your confusion seems to come from the fact that you don't realize that
sizeof(int), too, is implementation-defined (within limitations set up
by the standard on the range of values an int must at least be able to
hold). In theory, an implementation could have, say

CHAR_BITS == 13
sizeof(short) == 2
sizeof(int) == 3

Shouldn't sizeof(int) be 2 here?
As per the post by infobart :

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.

2 bytes here consisting of 26 bits can represent all these values so, it
should be 2 why 3 then?

-Neo
 
I

Ignacio G. T.

David said:
No, sizeof(char) is 1 even on DSP's that cannot access smaller than 16-bit
data. In the case of the TMS320F24x, sizeof(char) = sizeof(int), with
both being 16-bit. Makes the chip a real pain.

Mmm... yes, but, beware:
sizeof(char) = 1;
sizeof('c') = 1; // In C++
sizeof('c') = sizeof(int); /* In C */
 
T

Thomas Stegen

Neo said:
Shouldn't sizeof(int) be 2 here?
As per the post by infobart :

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.

2 bytes here consisting of 26 bits can represent all these values so, it
should be 2 why 3 then?

Because 3 bytes is also capable of representing all those values.
The standard defines a set of allowable sizes. That is all sizes
that can represent at least the above range of values.

Also (though this is not normative, just recommended practice)
int is intended to be the natural size for a given platform,
that means that for example a 32 bit machine will often have
sizeof(int) == 4 even though CHAR_BIT == 8.

IOW, the standard imposes a lower limit and does require that
the actual values are as close as possible to these limits.
 
M

Michael Mair

Neo said:
[F'up2 cut down --- should have been done by OP!]

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?

You cross-posted this question to two newsgroups, one of which (c.a.e)
it is seriously off-topic in. Please don't do that, or if you do, at
least explain why, and set a Followup-To. As is, your posting
silently assumes a context that only applies to half your audience,
causing all kinds of needless confusion.

In the context of comp.arch.embedded, your question doesn't make much
sense at all. In the context of comp.lang.c, the answers to the above
are: Yes. The same. Implementation-defined. (In that order).

Your confusion seems to come from the fact that you don't realize that
sizeof(int), too, is implementation-defined (within limitations set up
by the standard on the range of values an int must at least be able to
hold). In theory, an implementation could have, say

CHAR_BITS == 13
sizeof(short) == 2
sizeof(int) == 3


Shouldn't sizeof(int) be 2 here?

No. It could be.
As per the post by infobart :

Please give us some more information. Some people's newsserver may
not yet have that post, so a better reference is needed.
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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 bytes here consisting of 26 bits can represent all these values so, it
should be 2 why 3 then?

Parse correctly: _at_least_ versus _minimum_multiple_larger_than_
We only have
sizeof(int)*CHAR_BIT >= 16.

This does not disallow sizeof(int)*CHAR_BIT = 39.

In some cases, you may have a 64 bit machine and then you want to be
able to use the 64 bits, so an implementation might give you 64 bit
longs, 32 bit ints, 16 bit shorts, and 8 bit chars which makes much
more sense than the minimum requirements which leave you without a
64 bit integer data type (if we do not have C99's long long).

The limits are only the minimum requirements to enable you to write
portable programs.


:)


Cheers
Michael
 
M

Michael Mair

Thomas said:
Because 3 bytes is also capable of representing all those values.
The standard defines a set of allowable sizes. That is all sizes
that can represent at least the above range of values.

Also (though this is not normative, just recommended practice)
int is intended to be the natural size for a given platform,
that means that for example a 32 bit machine will often have
sizeof(int) == 4 even though CHAR_BIT == 8.

IOW, the standard imposes a lower limit and does require that
not
 

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,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top