By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

1 byte for character

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
ad************@gmail.com wrote:
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

Thanks,
Aditya

Nov 14 '05 #2

P: n/a
ad************@gmail.com wrote:
...
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.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3

P: n/a


Andrey Tarasevich wrote:
ad************@gmail.com wrote:
...
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.
...

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
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #4

P: n/a
Andrey Tarasevich <an**************@hotmail.com> writes:
ad************@gmail.com wrote:
...
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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

P: n/a
Keith Thompson wrote:
...
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.


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

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6

P: n/a
ad************@gmail.com wrote:
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.

--
Z (_Zoran_._Cutura_@_daimler_chrysler_.com) (remove underscores)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #7

P: n/a
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
Nov 14 '05 #8

P: n/a
ad************@gmail.com wrote:

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.

--
pete
Nov 14 '05 #9

P: n/a

"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...
ad************@gmail.com wrote:

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.


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

-Mike
Nov 14 '05 #10

P: n/a
"Mike Wahler" <mk******@mkwahler.net> writes:
"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...
ad************@gmail.com 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11

P: n/a
Keith Thompson wrote:
"Mike Wahler" <mk******@mkwahler.net> writes:
"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...
ad************@gmail.com 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.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #12

P: n/a
>Keith Thompson wrote:
Agreed, except for the "No". '\0' is less than or equal to CHAR_MAX.

In article <X-********************@comcast.com>
Joe Wright <jo********@comcast.net> wrote: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).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #13

P: n/a
Joe Wright <jo********@comcast.net> writes:
Keith Thompson wrote:
"Mike Wahler" <mk******@mkwahler.net> writes:
"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...

ad************@gmail.com 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14

P: n/a
Keith Thompson wrote:
Joe Wright <jo********@comcast.net> writes:
Keith Thompson wrote:
"Mike Wahler" <mk******@mkwahler.net> writes:
"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...
>ad************@gmail.com 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.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #15

P: n/a
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mike Wahler" <mk******@mkwahler.net> writes:
"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...
ad************@gmail.com 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
Nov 14 '05 #16

P: n/a
Mike Wahler wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mike Wahler" <mk******@mkwahler.net> writes:
"pete" <pf*****@mindspring.com> wrote in message
news:41***********@mindspring.com...

ad************@gmail.com 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #17

P: n/a
"Mike Wahler" <mk******@mkwahler.net> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mike Wahler" <mk******@mkwahler.net> writes:
> "pete" <pf*****@mindspring.com> wrote in message
> news:41***********@mindspring.com...
>> ad************@gmail.com 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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #18

P: n/a
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.

--
pete
Nov 14 '05 #19

P: n/a
pete <pf*****@mindspring.com> writes:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #20

P: n/a
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
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.


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.


I think he was confused about type int expressions
representing char values.
"But integers requires 2 bytes and the characters require only 1 byte."

--
pete
Nov 14 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.