473,782 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

signed char and unsigned char difference

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)
{
signed char a= 'a';
unsigned char b = 'b';
printf("%i %c",a,b);
return 0;
}

The above code does not warn about the assignment. I went through the
faq ,
Section 8 but I don't find the answer. Can any one give any pointer
regarding
the above subject?
Nov 14 '05 #1
9 4177
da***********@y ahoo.com wrote:
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.
Sorry, but these sentences don't make sense to me. For signed ints
the data range is INT_MIN to INT_MAX, for unsigned ints it's 0 to
UINT_MAX. INT_MIN must be at least -32767, INT_MAX +32767 and
UINT_MAX 65535. For long there are similar minimum ranges, with
"INT" replaced by "LONG" (i.e. LONG_MAX instead of INT_MAX) with
minimum requirements being LONG_MIN == -2^31-1, LONG_MAX == 2^31-1
and ULONG_MAX == 2^32-1. Implementations are allowed to support
larger ranges. The actual values can be found in <limits.h>.
But I don't understand why we have signed char which is -256.
The range for signed chars is SCHAR_MIN to SCHAR_MAX. Quite often
(on machines with 8 bits in a char and 2's complement) this is the
range between -128 and +127. The range for unsigned char is 0 to
UCHAR_MAX (quite often this is 0 to 255). The ranges of -127 to
127 for signed and 0 to 255 for unsigned chars are the minimum
requirements, so you can be sure you can store numbers from these
ranges wherever you have a standard compliant C compiler. While
there probably are some machines where you also could store -256
in a signed char you shouldn't rely on this, on many machines it
won't work.
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?
Yes, since ASCII characters are all in the range between 0 and 127,
thus they can always be stored in a signed as well as an unsigned
char.
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';


There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2

<Je***********@ physik.fu-berlin.de> wrote in message
news:2m******** ****@uni-berlin.de...
da***********@y ahoo.com wrote:

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?


Yes, since ASCII characters are all in the range between 0 and 127,
thus they can always be stored in a signed as well as an unsigned
char.
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';


There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).

It's been a while, but I once used a PRIMOS system where the default ASCII
representation had the high bit set.
Nov 14 '05 #3
On Sun, 25 Jul 2004 da***********@y ahoo.com wrote:
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.
First sentence doesn't make sense to me. The rest seems obviously true.
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?
The first sentence of this paragraph makes no sense to me. There are
systems where a char is 16 bits. For these systems you can have a signed
char with a value of -256. Maybe the confusion is that char is not used to
hold characters. It can be used for that purpose but it can also be used
as an integer data type with a very small range of values. If you need to
save space and you never need anything outside the range of a char, then
use a char.

As to your question, the ASCII character set is in the range 0 to 127. A
signed char is typically in the range -128 to 127. An unsigned char is
typically in the range 0 to 255. The ASCII character set will fit in both
ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
than 8 the range could be even larger, so it would still hold true.
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';
printf("%i %c",a,b);
return 0;
}
If you give printf a %i it is expecting an int. You are passing it a
signed char. This will have undefind behaviour. Did you mean to use:

printf("%c %c\n", a, b);
The above code does not warn about the assignment. I went through the
faq , Section 8 but I don't find the answer. Can any one give any
pointer regarding the above subject?


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #4
Je***********@p hysik.fu-berlin.de writes:
[...]
There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).


On any system that uses EBCDIC as the default encoding, plain char
will almost certainly be unsigned.

To oversimplify slightly:

Use plain char to hold characters (the implementation will have chosen
an appropriate representation) . Use unsigned char to hold bytes. Use
signed char to hold very small numeric values. (I actually haven't
seen much use for explicitly signed char.)

--
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 #5
On Sun, 25 Jul 2004 16:06:37 GMT, "Tim Prince"
<tp*****@nospam computer.org> wrote in comp.lang.c:

<Je***********@ physik.fu-berlin.de> wrote in message
news:2m******** ****@uni-berlin.de...
da***********@y ahoo.com wrote:

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?


Yes, since ASCII characters are all in the range between 0 and 127,
thus they can always be stored in a signed as well as an unsigned
char.
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';


There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).

It's been a while, but I once used a PRIMOS system where the default ASCII
representation had the high bit set.


Then it wasn't ASCII.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
On 25 Jul 2004 19:31:17 GMT, da*****@NOMORES PAMcs.utoronto. ca.com
(Darrell Grainger) wrote in comp.lang.c:
On Sun, 25 Jul 2004 da***********@y ahoo.com wrote:
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.


First sentence doesn't make sense to me. The rest seems obviously true.
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?


The first sentence of this paragraph makes no sense to me. There are
systems where a char is 16 bits. For these systems you can have a signed
char with a value of -256. Maybe the confusion is that char is not used to
hold characters. It can be used for that purpose but it can also be used
as an integer data type with a very small range of values. If you need to
save space and you never need anything outside the range of a char, then
use a char.

As to your question, the ASCII character set is in the range 0 to 127. A
signed char is typically in the range -128 to 127. An unsigned char is
typically in the range 0 to 255. The ASCII character set will fit in both
ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
than 8 the range could be even larger, so it would still hold true.
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';
printf("%i %c",a,b);
return 0;
}


If you give printf a %i it is expecting an int. You are passing it a
signed char. This will have undefind behaviour. Did you mean to use:


No, he is not. One can't pass any type of char as argument to a
variadic function beyond the specified ones. The char 'a' will be
promoted to int and the behavior is perfectly defined.

Technically, passing unsigned char 'b' to printf() with a "%c"
conversion specifier could be undefined because:

1. The implementation might have UCHAR_MAX > INT_MAX (in other words,
UCHAR_MAX == UINT_MAX) and so 'b' will be converted to unsigned,
rather than signed, int.

2. The standard suggests, but does not require, that the signed and
unsigned integer types be interchangeable as function argument and
return types.

So this just could be undefined on a platform where the character
types have the same number of bits as int (there are some, believe me)
and unsigned ints are passed to variadic functions differently than
signed ints are.

I would not hold my breath waiting for such an implementation to
appear. From a QOI point of view it would be horrible.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
da***********@y ahoo.com writes:
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.
True.
But I don't understand why we have signed char which is -256.
We don't.

The smallest value that fits in 8-bits (which is the minimal size a
signed char can hold, IIRC) is not -256 but -128. But your programs
shouldn't depend on that. Use SCHAR_MIN instead of an inline "magic"
value and you'll be fine ;-)
Does it means that we can assign the same ASCII value to both signed
and unsigned.
An unsigned char can hold values up to UCHAR_MAX. I'm not sure if
converting this value to signed char and back to unsigned will always
work as expected.
That means the ASCII value can be represented with a type of signed
char and also unsigned char?
No. SCHAR_MAX is usually 127 (if char values have 8-bits), which is
smaller than some of the values that unsigned characters can store.
For example

int main(void)
{
signed char a= 'a';
unsigned char b = 'b';
printf("%i %c",a,b);
return 0;
}

The above code does not warn about the assignment.


It depends on the warnings you have enabled. Here it doesn't even build
because printf() is called before a prototype is visible:

foo.c:5: warning: implicit declaration of function `printf'

Giorgos

Nov 14 '05 #8
Je***********@p hysik.fu-berlin.de wrote:
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';


There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).


The type 'char' has to be able to represent all members of the basic
character set, which includes 'a'. If the machine had 8-bit chars and
'a' == 129, then it must have 'char' being unsigned.
Nov 14 '05 #9
"Jack Klein" <ja*******@spam cop.net> wrote...
(Darrell Grainger) wrote in comp.lang.c:
On Sun, 25 Jul 2004 da***********@y ahoo.com wrote:
For example
int main(void)
{
signed char a= 'a';
Unless plain char is signed, there's no requirement that the value of 'a'
fit within the range of signed char.
unsigned char b = 'b';
printf("%i %c",a,b);
return 0;
}
If you give printf a %i it is expecting an int. You are passing it a
signed char. This will have undefind behaviour. Did you mean to use:


No, he is not. One can't pass any type of char as argument to a
variadic function beyond the specified ones. The char 'a' will be
promoted to int


This is ambiguous since a literal 'a' is already of int and no promotion
would be required. Of course, Jack is talking about the promotion of signed
and unsigned chars a and b respectively when used as arguments to printf.
and the behavior is perfectly defined.


--
Peter
Nov 14 '05 #10

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

Similar topics

4
1876
by: BigMan | last post by:
What is the difference between char and signed char, short and unsigned short, int and signed int, long and unsigned long?
6
14129
by: Sona | last post by:
Hi, What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one? If I do the following: char var; var = 0x00; what should var hold? and if it was an unsigned char, what should it hold then? Thanks
19
4679
by: Christopher Benson-Manica | last post by:
Given signed char str_a="Hello, world!\n"; unsigned char str_b="Hello, world!\n"; what is the difference, if any, between the following two statements? printf( "%s", str_a ); printf( "%s", str_b );
4
2662
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO lay out what should happen for: -------------------------- signed char sc; unsigned char uc;
10
15664
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 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 */
4
1589
by: Sriniv | last post by:
Hello all, Do we have unsigned and signed char types? If so, i'd like to know the differences between the two. And one more question. Is there any provision in C for using the Unicode character set? Thank you. -- Srinivasan.
20
5365
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>
8
4066
by: Marcin Kalicinski | last post by:
Are 3 types: signed char, char and unsigned char distinct? My compiler is treating char as signed char (i.e. it has sign, and range from -128 to 127), but the following code does not call f<char> as I would expect: template<class T> void f(T t) { } template<> void f<char>(char t) {
9
469
by: Ioannis Vranos | last post by:
Under C++03: Is it guaranteed that char, unsigned char, signed char have no padding bits?
0
9639
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
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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
10080
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
8967
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
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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...
3
2874
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.