473,320 Members | 2,000 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,320 software developers and data experts.

char and unsigned char

Hi,

char is 8 bit from -127 to 127
unsigned char is 8 bit from 0-255

from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?
If I declare "char x", then can x hold char from 128-255 ?

If want to hold value from 0-127 only?

Thanks.
Nov 14 '05 #1
9 8764
j

"Magix" <ma***@asia.com> wrote in message news:41********@news.tm.net.my...
Hi,

char is 8 bit from -127 to 127
unsigned char is 8 bit from 0-255
char is CHAR_BIT bits, where CHAR_BIT can be more than eight.

from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?
It is up to the implementation to decide whether
``char'' is ``unsigned'' or ``signed''.
If I declare "char x", then can x hold char from 128-255 ?
Maybe, if ``char'' is implemented as ``unsigned char'' with your
particular implementation.

If want to hold value from 0-127 only?


Then use unsigned char.

--
j
Nov 14 '05 #2
j

"j" <ja**********@bellsouth.net> wrote in message
news:EM********************@bignews3.bellsouth.net ...

"Magix" <ma***@asia.com> wrote in message

news:41********@news.tm.net.my...
Hi,

char is 8 bit from -127 to 127
unsigned char is 8 bit from 0-255


char is CHAR_BIT bits, where CHAR_BIT can be more than eight.

from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?


It is up to the implementation to decide whether
``char'' is ``unsigned'' or ``signed''.
If I declare "char x", then can x hold char from 128-255 ?


Maybe, if ``char'' is implemented as ``unsigned char'' with your
particular implementation.

If want to hold value from 0-127 only?


Then use unsigned char.


I should also say that the standard does not dictate a range
with respect to the values of an unsigned char, or signed char.

It dictates an upper bound though, which is CHAR_BIT bits.
Your implementation is free to implement a trivial mapping
scheme so that char foo = 88; can very well be 600+foo.

--
j
Nov 14 '05 #3
In article <41********@news.tm.net.my>, "Magix" <ma***@asia.com> wrote:
Hi,

char is 8 bit from -127 to 127
No -- char on 8-bit 2's complement machines is either -128 to 127 or
0-255, depending on the C implementation.
unsigned char is 8 bit from 0-255

from ASCII table, there is a list of char from 0-255. How char can be in
negative value ?
<slightly OT>
You can view any 8-bit number as either an "unsigned" number from
0->255, or as a "signed" number from 0->127 and -128 -> -1. Most every
computer chip nowadays uses the "2's complement" representation for
negative numbers. The algorithm for inverting (i.e. multiplying by -1) a
value 'x' in the two's complement representation is:

flip all of the bits of x
add 1

or, in C notation:

(~x + 1) == -x

In this representation, unsigned "128" == signed "-128", and
unsigned "255" == signed "-1".
</slightly OT>

If I declare "char x", then can x hold char from 128-255 ?
Depends on your platform -- char x could either be signed or unsigned.
If want to hold value from 0-127 only?


Make sure you don't put anything larger than 127 into it.

- jonathan
Nov 14 '05 #4
Magix wrote:
Hi,

char is 8 bit from -127 to 127 Wrong. signed char has a range of at least -127 to +127.
char may be either signed char or unsigned char.
No variety of char is constrained to be 8 bits.
unsigned char is 8 bit from 0-255 Wrong. unsigned char has a range of at least 0 to 255 and is not
constrained to be 8 bits.

from ASCII table, there is a list of char from 0-255.
Wrong. ASCII extends only from 0 to 127. There are a wide variety of
so-called extensions into the range 128-255, but they greatly differ
from one another and are none of them ASCII.
How char can be in
negative value ?
If I declare "char x", then can x hold char from 128-255 ?
If you declare "unsigned char x" then x can have a range from 128-255,
but those codes do not represent ASCII encodings.

If want to hold value from 0-127 only?


Since the ranges for both signed and unsigned chars, and hence unadorned
chars, include every value in 0-127, use whatever kind of char you want.
If you intend to do bit arithmetic (masking, anding, etc.) use
unsigned char.

Nov 14 '05 #5
j wrote:

"j" <ja**********@bellsouth.net> wrote in message
news:EM********************@bignews3.bellsouth.net ...

"Magix" <ma***@asia.com> wrote in message

news:41********@news.tm.net.my...
Hi,

char is 8 bit from -127 to 127
unsigned char is 8 bit from 0-255


char is CHAR_BIT bits, where CHAR_BIT can be more than eight.

from ASCII table, there is a list of char from 0-255.
How char can be in
negative value ?


It is up to the implementation to decide whether
``char'' is ``unsigned'' or ``signed''.
If I declare "char x", then can x hold char from 128-255 ?


Maybe, if ``char'' is implemented as ``unsigned char'' with your
particular implementation.

If want to hold value from 0-127 only?


Then use unsigned char.
The standard dictates minimum ranges of types, and 0-127 inclusive,
is within the minimum guaranteed range of char.


I should also say that the standard does not dictate a range
with respect to the values of an unsigned char, or signed char.


I wouldn't use a smaller integer type than int,
unless there was a special reason,
like if I was going to store many of them in an array.

--
pete
Nov 14 '05 #6

"j" <ja**********@bellsouth.net> wrote in message
news:EM********************@bignews3.bellsouth.net ...

"Magix" <ma***@asia.com> wrote in message

news:41********@news.tm.net.my...

If want to hold value from 0-127 only?


Then use unsigned char.


And if you need to specifically disallow values > 127,
do so in your code (use 'if', or mask the possible offending bits
away).

-Mike
Nov 14 '05 #7
"j" <ja**********@bellsouth.net> wrote:

I should also say that the standard does not dictate a range
with respect to the values of an unsigned char, or signed char.
Actually it does, if you look in the specification for <limits.h>

SCHAR_MIN must be at most -127
SCHAR_MAX must be at least 127
UCHAR_MAX must be at least 255
It dictates an upper bound though, which is CHAR_BIT bits.
Your implementation is free to implement a trivial mapping
scheme so that char foo = 88; can very well be 600+foo.


No it isn't. The value must be 88. I think you are trying
to say that the representation can be something other than
pure binary. In C99 this is wrong, see 6.2.6.1/3 and 6.2.6.2/2.
I don't know if C90 has this same restriction (but would be
surprised if there were any implementations that broke this rule).
Nov 14 '05 #8
On Mon, 18 Oct 2004 00:27:10 -0700, Jonathan Adams <jw*****@gmail.com>
wrote in comp.lang.c:
In article <41********@news.tm.net.my>, "Magix" <ma***@asia.com> wrote:
Hi,

char is 8 bit from -127 to 127


No -- char on 8-bit 2's complement machines is either -128 to 127 or
0-255, depending on the C implementation.


No, signed char on 8 bit 2's complement machines is only required to
hold the range of values -127 to 127 inclusive. It is not required to
hold the value -128. The current C standard specifically states that
on object of a signed integer type on two's complement platforms that
has the sign bit 1 and all value bits 0 may be a trap representation.

Specifically see section 6.2.6.2 paragraphs 1 and 2.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
In article <tj********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Mon, 18 Oct 2004 00:27:10 -0700, Jonathan Adams <jw*****@gmail.com>
wrote in comp.lang.c:
In article <41********@news.tm.net.my>, "Magix" <ma***@asia.com> wrote:
Hi,

char is 8 bit from -127 to 127


No -- char on 8-bit 2's complement machines is either -128 to 127 or
0-255, depending on the C implementation.


No, signed char on 8 bit 2's complement machines is only required to
hold the range of values -127 to 127 inclusive. It is not required to
hold the value -128. The current C standard specifically states that
on object of a signed integer type on two's complement platforms that
has the sign bit 1 and all value bits 0 may be a trap representation.

Specifically see section 6.2.6.2 paragraphs 1 and 2.


Ah, my mistake. Apologies,

- jonathan
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
7
by: techno | last post by:
Dear all, Our bitmap has some x00 values ( '\0' ) and i am storing it in char* array. the problem is that the '\0' is treated as eos character in c and it is truncating it so the characters...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
6
by: zhangsonglovexiaoniuniu | last post by:
Dear all, I need you help. here the program: char a = 0x91; printf("%x",a);
20
by: =?Utf-8?B?ZW1pdG9qbGV5ZXM=?= | last post by:
Hi everyone: i read from the documentation of a dll that there is a struct that uses char for storing an IP address. How can it be? and how come i can get to representate the same value in a...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.