1 byte for character

A

adityavasishth

hi all,

Characters are basically implemented via integers,ex : '\0' is 0.But
integers requires 2 bytes and the characters require only 1
byte.So,can anybody please tell me that how the charcters are
implemented via integers.

Thanks,
Aditya
 
Y

Yan Ivnitskiy

hi all,

Characters are basically implemented via integers,ex : '\0' is 0.But
integers requires 2 bytes and the characters require only 1
byte.So,can anybody please tell me that how the charcters are
implemented via integers.

let's say you assign 1 to a 2-byte int (i think they're 4 bytes on most
x86 machines)
int a = 1;
now, a will, in binary, be: 00000000 00000000 00000000 00000001
if you assign that to a char, i.e.
char a = 1;, you'll just have 00000001 in memory

So you can assign the value to the actual bytes, but you can't assign
value that go outside the range, i.e. -128 to 127
 
A

Andrey Tarasevich

...
Characters are basically implemented via integers,ex : '\0' is 0.But
integers requires 2 bytes and the characters require only 1
byte.So,can anybody please tell me that how the charcters are
implemented via integers.
...

The answer is very simple: characters are NOT implemented via integers.
(Where did you get this idea?) For this reason your question can be
dismissed as based on an invalid premise.
 
M

Michael Mair

I think you should clarify your question.
Some things to help you along:
- character constants, such as '0', '\0', ' ', 'a' are of type
int (as they are only seen as a different representation of
an integral number) but are guaranteed to "fit" into a char.
- chars consist of CHAR_BIT bits.
- ints consist of sizeof(int) chars and consequently of
sizeof(int)*CHAR_BIT bits. sizeof(int)>=1. It can be 1,2,4
or whatever makes sense on the respective platform and for
the implementation.
Now, do you want to know how to "extract" sizeof(int) different
chars from an int or do you want to know how the mapping
between numbers and characters works or was this information
enough for you or ...?
The answer is very simple: characters are NOT implemented via integers.
(Where did you get this idea?) For this reason your question can be
dismissed as based on an invalid premise.

Depends. If the processor word size and minimal addressable
unit is >=16 Bit, the implementation could decide to give
you a char type implemented in software. Whether that is also
what the OS (if any) "does" ... *shrug*.


Cheers
Michael
 
K

Keith Thompson

Andrey Tarasevich said:
The answer is very simple: characters are NOT implemented via integers.
(Where did you get this idea?) For this reason your question can be
dismissed as based on an invalid premise.

Andrey is mistaken; characters are implemented as integers. (He may
have meant that characters are not implemented as ints; read on.)

In particular, the types "char", "signed char", and "unsigned char"
are integer types. (Other integer types include short, int, long, and
their unsigned variants.)

Note the distinction between the term "integer", which covers a
variety of types, and the term "int", which is one particular integer
type.

The type "int" can be 2 bytes on some systems. It can be 4 or more
bytes on other systems. A byte (as C uses the term) is at least 8
bits, but can be larger. If a byte is at least 16 bits, an int can be
a single byte, though this is rare.

So a character (type "char") is an integer, it's just a relatively
small one.

For more information, see any good C textbook. Kernighan & Ritchie's
_The C Programming Language_, 2nd Edition, is excellent. I also
recommend the C FAQ, <http://www.eskimo.com/~scs/C-faq/faq.html>.
 
A

Andrey Tarasevich

Keith said:
Andrey is mistaken; characters are implemented as integers. (He may
have meant that characters are not implemented as ints; read on.)

You are right. Thans for the correction. I was subconsciously sticking
to the terminology used by the OP. Apparently the OP also used the term
"integers" to mean "ints" ("... integers requires 2 bytes ...").
 
Z

Zoran Cutura

hi all,

Characters are basically implemented via integers,ex : '\0' is 0.But
integers requires 2 bytes and the characters require only 1
byte.So,can anybody please tell me that how the charcters are
implemented via integers.

Whoever told you that integers are 2 bytes lied. And whoever told you
that characters are only 1 byte large did too.

The type char although its name probably implies something else is a
integer type. In C a char is by definition 1 byte large. This must not
be confused with characters in the sense of written letters or symbols.
Also note that 1 byte in C need not necessarily be 8 bits, but in many
implementations actually is.

A character on the other hand is something the implementation (your
computer in conjunction with the used compiler and environment) is to
interpret on one way or the other. How characters are interpreted is not
actually defined bye the C-standard. There are only some constraints
given to the implementation, so that a basic set of characters is made
available by all implementations claiming conformance.

One of the constraints given by the standard is that the representation
of a character from that basic set must fit into a byte, which is it
must fit in to a variable of type "char". But as you probably can
imagine a implementation might give you some optional characters that
would extend the basic set. The represetations of such characters need
not fit into a byte/char. But if the implementation should be able to
allow for literals in your programs for example it should also be able
to store characters into integer variables that are not of type char but
bigger. Therefor literal characters are of type int which is a integer
type that is as big or bigger than short, which depending on the
implementation might mean that it is 1 byte (if a byte would consist of
at least 16 value bits) large but might also be 2, 3, 4, or any other.

You might have recopgnized that a value that can be stored in 1 byte
can always be stored in two bytes or more.

I hope this doesn't confuse you even more, but if it does, don't
hasitate to ask for more clearification, I'm sure some regulars in CLC
will point you in the right direction.
 
L

Lawrence Kirby

On Thu, 09 Dec 2004 19:19:26 +0000, Yan Ivnitskiy wrote:

....
let's say you assign 1 to a 2-byte int (i think they're 4 bytes on most
x86 machines)
int a = 1;
now, a will, in binary, be: 00000000 00000000 00000000 00000001
if you assign that to a char, i.e.
char a = 1;, you'll just have 00000001 in memory

So you can assign the value to the actual bytes, but you can't assign
value that go outside the range, i.e. -128 to 127

That is one possible range of values for the type char, but there are
other. char can be wider than 8 bits, even when it is 8 bits it can have
an unsigned representation with the range 0 to 255. If it is signed and 8
bits the range could be -127 to 127.

Lawrence
 
P

pete

hi all,

Characters are basically implemented via integers,ex : '\0' is 0.But
integers requires 2 bytes and the characters require only 1
byte.So,can anybody please tell me that how the charcters are
implemented via integers.

The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.
 
M

Mike Wahler

pete said:
The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.

-Mike
 
K

Keith Thompson

Mike Wahler said:
pete said:
(e-mail address removed) wrote: [...]
The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.

Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.
 
J

Joe Wright

Keith said:
Mike Wahler said:
(e-mail address removed) wrote:
[...]
The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.


Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

Please expand on the case for '\0' == CHAR_MAX. Thanks.
 
C

Chris Torek

Please expand on the case for '\0' == CHAR_MAX. Thanks.

He is saying that, mathematically:

0 <= 127

is just as correct as:

0 < 127

(which is true -- both statements are true).
 
K

Keith Thompson

Joe Wright said:
Keith said:
Mike Wahler said:
(e-mail address removed) wrote: [...]

The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.
Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

Please expand on the case for '\0' == CHAR_MAX. Thanks.

Please expand on the case that '\0' is *not* less than or equal to
CHAR_MAX.
 
J

Joe Wright

Keith said:
Joe Wright said:
Keith said:
(e-mail address removed) wrote:

[...]


The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.

Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

Please expand on the case for '\0' == CHAR_MAX. Thanks.


Please expand on the case that '\0' is *not* less than or equal to
CHAR_MAX.

I got it. Thanks.
 
M

Mike Wahler

Keith Thompson said:
Mike Wahler said:
pete said:
(e-mail address removed) wrote: [...]
The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.

Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

Huh? How can '\0' be equal to CHAR_MAX, When CHAR_MAX
is required to be >= 127?

-Mike
 
M

Michael Mair

Mike said:
Mike Wahler said:
(e-mail address removed) wrote:
[...]

The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.

Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

Huh? How can '\0' be equal to CHAR_MAX, When CHAR_MAX
is required to be >= 127?

See the other subthread. Keith just states
0 <= CHAR_MAX
in a non-unambigous way. It is true but certainly weaker
than 0 < CHAR_MAX.


Cheers
Michael
 
K

Keith Thompson

Mike Wahler said:
Keith Thompson said:
Mike Wahler said:
(e-mail address removed) wrote: [...]
The type of the expression ('\0'), is int.
The value of '\0', is an integer value which the C standard
guarantees is greater than or equal to CHAR_MIN
and also less than or equal to CHAR_MAX.

No, '\0' is never equal to CHAR_MAX. It's always at
least 127 less than CHAR_MAX.

Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

Huh? How can '\0' be equal to CHAR_MAX, When CHAR_MAX
is required to be >= 127?

We settled this several days ago. '\0' is less than or equal to
CHAR_MAX. If you disagree, show a case where '\0' is *not* less than
or equal to CHAR_MAX. (I never said it was equal.)
 
P

pete

Michael Mair wrote:
See the other subthread. Keith just states
0 <= CHAR_MAX
in a non-unambigous way. It is true but certainly weaker
than 0 < CHAR_MAX.

OP's question was
"tell me that how the charcters are implemented via integers"

The answer to the question,
is that character constants are of type int
and they have values from CHAR_MIN to CHAR_MAX.

The particular restrictions of individual character constants
such as '0' is greater CHAR_MIN and less than CHAR_MAX - 8
would just have been distracting.
 
K

Keith Thompson

pete said:
OP's question was
"tell me that how the charcters are implemented via integers"

The answer to the question,
is that character constants are of type int
and they have values from CHAR_MIN to CHAR_MAX.

I don't think that's the point. Character *constants* are of type
int, but objects of type char are (obviously) of type char (which is
an integer type), which I think is more relevant to what the OP was
asking.

Just to add to the frivolity, there was also some confusion about the
distinction between "integer" and "int". int is a single specific
type; integer types include int, short, long, and the character types.
 

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,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top