size of a byte and int?

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

Reply to
Neo
Loading thread data ...

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

--
pete
Reply to
pete

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

-Neo

Reply to
Neo

Yes.

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

It will be whatever it is on a given implementation.

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
Reply to
Peter Nilsson

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

Reply to
infobahn

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

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use @monlynx.de instead !
Reply to
42Bastian Schick

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

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

^^^ 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 @monlynx.de instead !
Reply to
42Bastian Schick

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
Reply to
Paul Black

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

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.

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

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
Reply to
Peter Nilsson
[-snip-]

Why not conforming? Then why does the std. defines these?

-Neo

Reply to
Neo

(They are not _strictly_ conforming)

Because a platform is not required to provide them. Just in case it cannot support them.

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.

--
Thomas.
Reply to
Thomas Stegen

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.

Reply to
David

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.

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

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

Reply to
Ignacio G. T.

Nope. sizeof (char) is 1. It doesn't matter if the char has

8, 16, or 32 bits.
--
Grant Edwards                   grante             Yow!  does your DRESSING
                                  at               ROOM have enough ASPARAGUS?
                               visi.com
Reply to
Grant Edwards

Common sense is often wrong. As yours seems to be in this case. I've worked on architectures where a byte (a-la "C") was

32 bits.

The OP is asking about C. In C, 'byte' has a very specific definition.

--
Grant Edwards                   grante             Yow!  Yow! I like my new
                                  at               DENTIST...
                               visi.com
Reply to
Grant Edwards

snipped-for-privacy@yahoo.com (42Bastian Schick) wrote in news: snipped-for-privacy@news.individual.de:

No, but maybe you're thinking of CHAR_BITS?

--
- Mark ->
--
Reply to
Mark A. Odell

snipped-for-privacy@yahoo.com (42Bastian Schick) wrote in news: snipped-for-privacy@news.individual.de:

Leading underscores are for the implementation, not application programs.

--
- Mark ->
--
Reply to
Mark A. Odell

A byte is ALWAYS 8 bits. A word is usually the larger of 16 or whatever the width of the processor bus.

Reply to
Mike Harrison

Not in C. In C, a byte is the smallest addressible unit of memory.

I don't believe that "word" has a definition in C.

--
Grant Edwards                   grante             Yow!  Hey, LOOK!! A pair of
                                  at               SIZE 9 CAPRI PANTS!! They
                               visi.com            probably belong to SAMMY
                                                   DAVIS, JR.!!
Reply to
Grant Edwards

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.