473,770 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the difference between signed and unsigned char?

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 standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */

putchar(a);
putchar(b); /* both print the same character (ex ascii 254)*/

-------------
It seems to me that it doesn't matter whether char is signed or
unsigned, because the output functions just look at the bit pattern and
deal with it as a positive number.
Also, I assigned a negative number to unsigned char, it wraps around
and creates the same bit pattern as assigning the same negative number
to signed char.

So my question is, what really is the difference between unsigned and
signed char?

Also, for other integral types, are the normal types always equal to
the signed types (int = signed int, long = signed long,etc. etc.)... or
is that implementation defined just like for chars?
Any help will be appreciated.

Nov 14 '05 #1
10 15663

tine...@gmail.c om wrote:
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */


I don't think you can assign a negative initializer to a signed
integer.
Am I right people?

Nov 14 '05 #2
The other way around...
unsigned char does not have a sign extension.
On 1/27/05 4:21 PM, in article
11************* ********@c13g20 00...legro ups.com, "Kobu"
<ko********@gma il.com> wrote:

tine...@gmail.c om wrote:
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 standard doesn't even care what a normal

char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */


I don't think you can assign a negative initializer to a signed
integer.
Am I right people?


Nov 14 '05 #3
<ti*****@gmail. com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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 standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
In this, the value -2, of type int, is converted to unsigned char. This
conversion is specified as being equivalent to repeatedly adding or
subtracting UCHAR_MAX + 1 (where UCHAR_MAX is the maximum value an unsigned
char can have; apparently 255 for your compiler) until the result is between
0 and UCHAR_MAX inclusive. So a is assigned the value 254.
signed char b = -2; /* or = 254 */
Here, -2, again of type int, is converted to signed char. Note however that
the effect of assigning 254 to b (which can hold values between SCHAR_MIN
and SCHAR_MAX inclusive, probably -128 and 127 respectively in your case) is
undefined by the standards.
putchar(a);
putchar(b); /* both print the same character (ex ascii 254)*/
The putchar function takes an int, so for both these calls, the argument is
converted to type int; the calls are equivalent to putchar(254) and
putchar(-2) respectively. The putchar function is specified as converting
its parameter to unsigned char, which uses the rule above. Therefore, with
UCHAR_MAX being 255, the second call is equivalent to the first in terms of
the result.
It seems to me that it doesn't matter whether char is signed or
unsigned, because the output functions just look at the bit pattern and
deal with it as a positive number.
See above.
Also, I assigned a negative number to unsigned char, it wraps around
and creates the same bit pattern as assigning the same negative number
to signed char.
See the rule above. The wrapping around is what the standards specify. The
fact it is the same bit pattern is common, because two's complement
representation for signed numbers is common, but two's complement is not
required by the standards.
So my question is, what really is the difference between unsigned and
signed char?
One can represent unsigned values, and the other signed values (obviously).
As indicated above, conversion of out-of-range values to signed types (such
as signed char) is undefined by the standard. Similarly, the result of
arithmetic that produces out-of-range values for the type is undefined. But
the behaviour in both these cases for unsigned types (such as unsigned char)
*is* defined.
Also, for other integral types, are the normal types always equal to
the signed types (int = signed int, long = signed long,etc. etc.)... or
is that implementation defined just like for chars?


char can represent the same range of values as either signed char or
unsigned char, but all three are distinct types. (Similarly, int and long
may be able to represent the same range of values on a given implementation,
but they too are distinct types.)

int, short, long (and long long in C99) are always capable of representing
negative numbers. I think that int and signed int are the same type, and
similarly for short, long and long long. Hopefully someone else can clarify
this point.

HTH,
Alex
Nov 14 '05 #4
ti*****@gmail.c om wrote:

For example if someone does this:

unsigned char a = -2; ...
signed char b = -2; ...

putchar(a);
putchar(b); /* both print the same character (ex ascii 254)*/
Abstractly, putchar() is a wrapper for fputc(), and...

"The fputc function writes the character specified by c
(converted to an unsigned char)..."

So, the calls will both send the same byte value to stdout.
-------------
It seems to me that it doesn't matter whether char is signed
or unsigned, because the output functions just look at the bit
pattern and deal with it as a positive number.
No, the conversion is _NOT_ specified in terms of bit pattern.

On a signed magnitude machine, the 8-bit representation of -2 is...

10000010

Irrespective of the signed char representation, the conversion
will always yield UCHAR_MAX + 1 - 2.
Also, I assigned a negative number to unsigned char, it wraps
around and creates the same bit pattern as assigning the same
negative number to signed char.
It will do so on two's complement machines. However, the standard
doesn't _require_ two's complement integer representation for
negative signed integers.
So my question is, what really is the difference between unsigned
and signed char?

Also, for other integral types, are the normal types always equal
to the signed types (int = signed int, long = signed long,etc.
etc.)...
Yes.
or is that implementation defined just like for chars?


No.

--
Peter

Nov 14 '05 #5
Gregory Dean wrote:
The other way around...
unsigned char does not have a sign extension.
Please don't top post in clc.
ko********@gmai l.com> wrote:

I'm just learning to program with C, and I'm wondering what
the difference between signed and unsigned char is.
Apart from the obvious!?
To me there seems to be no difference, and the standard
doesn't even care what a normal char is (because signed and
unsigned have equal behavior).
You haven't read the standard, have you?

The value of an unsigned integer cannot be negative.
For example if someone does this:

unsigned char a = -2; /* or = 254 */
/* or UCHAR_MAX + 1 - 2 */
signed char b = -2; /* or = 254 */

Since -2 is in the range -127..127, the minimum range for
signed char, b will always be assigned the value -2 here
on any conforming implementation.
I don't think you can assign a negative initializer to a signed
integer.

Am I right people?


No.

--
Peter

Nov 14 '05 #6
"Kobu" <ko********@gma il.com> writes:
tine...@gmail.c om wrote:
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 standard doesn't even care what a normal

char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */


I don't think you can assign a negative initializer to a signed
integer.
Am I right people?


No. Both declarations above are legal.

--
Keith Thompson (The_Other_Keit h) 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 #7
On 27 Jan 2005 12:27:18 -0800, ti*****@gmail.c om
<ti*****@gmail. com> wrote:
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 standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */

putchar(a);
putchar(b); /* both print the same character (ex ascii 254)*/
On your machine. They won't on a 1-s complement machine. Incidentally,
value 254 (0xFE) is not an ASCII character, ASCII only defines 7 bit
characters in the range 0x00 to 0x7FF (0 to 127).
-------------
It seems to me that it doesn't matter whether char is signed or
unsigned, because the output functions just look at the bit pattern and
deal with it as a positive number.
Usually, yes. Not guaranteed, though (some output libraries will fail
if the parameter is negative).
Also, I assigned a negative number to unsigned char, it wraps around
and creates the same bit pattern as assigning the same negative number
to signed char.
Again, on your machine.
So my question is, what really is the difference between unsigned and
signed char?
int main(void)
{
unsigned char a = -2;
signed char b = -2;

if (a == b)
printf("equal\n ");

if (a > 0)
printf("a > 0\n");

if (b > 0)
printf("b > 0\n");
return 0;
}

What, if anything, will be output?
Also, for other integral types, are the normal types always equal to
the signed types (int = signed int, long = signed long,etc. etc.)... or
is that implementation defined just like for chars?


They are defined to take the same amount of storage. It is also defined
that a signed value which is positive can be converted to an unsigned
value of the same type and back again with no change in value. However,
an unsigned value which is greater than the maximum positive value which
a signed version can hold cannot be reliably converted into a signed
value, nor is it defined what value an unsigned version of a negative
value has.

Chris C
Nov 14 '05 #8
Chris Croughton wrote:
.... snip on signed/unsigned char ...
They are defined to take the same amount of storage. It is also defined
that a signed value which is positive can be converted to an unsigned
value of the same type and back again with no change in value. However,
an unsigned value which is greater than the maximum positive value which
a signed version can hold cannot be reliably converted into a signed
value, nor is it defined what value an unsigned version of a negative
value has.


That last provision is wrong. There is a specific process for
converting any out-of-range value to an unsigned value. It just
isn't necessarily reversible.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #9
On 1/27/05 6:01 PM, in article
11************* *********@f14g2 00...legr oups.com, "Peter Nilsson"
<ai***@acay.com .au> wrote:
Gregory Dean wrote:
The other way around...
unsigned char does not have a sign extension.

lly pre
Please don't top post in clc.
ko********@gmai l.com> wrote:

I'm just learning to program with C, and I'm wondering what
the difference between signed and unsigned char is.
Apart from the obvious!?
To me there seems to be no difference, and the standard
doesn't even care what a normal char is (because signed and
unsigned have equal behavior).
You haven't read the standard, have you?

The value of an unsigned integer cannot be negative.
For example if someone does this:

unsigned char a = -2; /* or = 254 */
/* or UCHAR_MAX + 1 - 2 */
signed char b = -2; /* or = 254 */
Since -2 is in the range -127..127, the minimum range for
signed char, b will always be assigned the value -2 here
on any conforming implementation.
I don't think you can assign a negative initializer to a signed
integer.

Am I right people?


No.


Silly preferences setting in Entourage 2004. Sorry.
-Greg

Nov 14 '05 #10

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

Similar topics

19
6481
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
3
31511
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
9
4177
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have signed char which is -256. Does it means that we can assign the same ASCII value to both signed and unsigned. That means the ASCII value can be represented with a type of signed char and also unsigned char? For example int main(void) {
39
4472
by: August Karlstrom | last post by:
Hello all, If c is a char then is there any difference between c = '\0' and c = 0
11
2971
by: Jason Curl | last post by:
Dear C people, C90 doesn't specify if 'char' is either 'signed char' or 'unsigned char' leaving it to the implementation of the compiler. Has this changed for C99, or is it still the same? Thanks, Jason.
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok etc and their implementation.the way compiler treats them and the scenarios where one
35
5471
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : %d\n",sizeof(unsigned char)); printf("short : %d\n",sizeof(short)); printf("unsigned short : %d\n",sizeof(unsigned short)); printf("int : %d\n",sizeof(int));
20
5364
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
48
3376
by: Frederick Gotham | last post by:
The "toupper" function takes an int as an argument. That's not too irrational given that a character literal is of type "int" in C. (Although why it isn't of type "char" escapes me... ) The "toupper" function imposes a further constrait in that the value passed to it must be representable as a unsigned char. (If C does not require all character values to be positive, then again, this constrait too escapes me... ) Let's say we have the...
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.