473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

1 byte for character

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
20 2661
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
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


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

"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
"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
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
>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 (40°39.22'N, 111°50.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
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
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
"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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dorthe Luebbert | last post by:
Hi, we have to convert a quite large ISO-application (Mysql4, PHP5) to UTF8. We found out so far that there is no collation for MySQL which is able to sort all character sets correctly. So this...
43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
43
by: Bill Cunningham | last post by:
I've been reading the C standard online and I'm puzzled as to what multibyte chars are. Wide chars I believe would be characters for languages such as cantonese or Japanese. I know the ASCII...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
1
by: Steve Marshall | last post by:
Hi all, This is probably a real dumb question, but I just haven't come across the answer... Is there a simple way to treat a byte array as a string, or to convert it to a string? And the...
6
by: Deep | last post by:
Suppose there is a character of one byte then max characters possible under this are 256. If characters are of two bytes then max chars should be 65536. Now if character can be of one byte or two...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
4
jeffbroodwar
by: jeffbroodwar | last post by:
Hello, i have a problem about assigning a char value to a byte... please check the code below : ======================================================== Scenario # 1 : This code doesn't work : ...
198
by: Antoninus Twink | last post by:
Or, just how low has this group sunk? Twice in the past couple of days, Default Loser has accused Chris Hills, member of the ISO C committee, of being a troll (though given that the former's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.