473,804 Members | 3,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

signed and unsigned char

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

If there is a difference, what is the best way to compare *str_a with
0xFF? (On my implementation, unadorned char is signed, and so I'm
using

if( *str_a == (signed char)0xFF ) ...

to quiet compiler warnings.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05
19 4682
pete <pf*****@mindsp ring.com> wrote:
Christopher Benson-Manica wrote:

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

If there is a difference, what is the best way to compare *str_a with
0xFF?


char type arguments are converted to int.


They're not char type arguments, they're _pointers_ to char.

AFAICT pointers to signed and unsigned types must behave the same, btw.
(On my implementation, unadorned char is signed, and so I'm
using

if( *str_a == (signed char)0xFF ) ...


Not perfectly guaranteed to work. Conversion of unsigned values to
signed types, when they're out of range, is implementation-defined. It's
not undefined, which means that at least it's guaranteed not to give
false negatives (0xFF must be converted to _some_ signed char value, not
to random junk), but it may give false positives (AFAICT, it's legal for
some other value to convert to the same signed char as 0xFF).

if ((unsigned char)*str_a == 0xFF)

should be perfectly defined, but will not do what you expect if chars
are more than eight bits. OTOH,

if ((unsigned char)*str_a == UCHAR_MAX)

is also well-defined, but will not compare to 0xFF when CHAR_BIT > 8.

Richard
Nov 14 '05 #11
Eric Sosman <Er*********@su n.com> wrote:
Christopher Benson-Manica wrote:
(On my implementation, unadorned char is signed, and so I'm
using

if( *str_a == (signed char)0xFF ) ...


Undefined behavior, I think.


Implementation-defined, surely?

Richard
Nov 14 '05 #12
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Christopher Benson-Manica wrote:

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

If there is a difference, what is the best way to compare *str_a with
0xFF?


char type arguments are converted to int.


They're not char type arguments, they're _pointers_ to char.


I was considering printf, as a byte output function,
defined in terms of fputc as per Jeremy Yallop's
last post in this thread.

http://groups.google.com/groups?selm...e.cl.cam.ac.uk

--
pete
Nov 14 '05 #13
pete <pf*****@mindsp ring.com> spoke thus:
If str_a points to an all bits set byte,
then *(unsigned char*)str_a will equal ((unsigned char)-1)


Is that guaranteed? (non-rhetorical question)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #14
Christopher Benson-Manica wrote:

pete <pf*****@mindsp ring.com> spoke thus:
If str_a points to an all bits set byte,
then *(unsigned char*)str_a will equal ((unsigned char)-1)


Is that guaranteed? (non-rhetorical question)


Yes.
For the type unsigned char, there are only value bits;
there are no padding bits and there is no sign bit.
The expression
*(unsigned char*)str_a
will cause the byte at str_a,
to be evaluated by it's bit pattern,
according to the rules for the representation of unsigned char.

(-1) cast to any unsigned type,
is the MAX value for that unsigned type,
which is, all value bits, set to one.

--
pete
Nov 14 '05 #15
In <c0**********@c hessie.cirr.com > Christopher Benson-Manica <at***@nospam.c yberspace.org> writes:
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 );
None.

s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type.
^^^^^^^^^^^^^^
Both str_a and str_b are arrays of character type.
If there is a difference, what is the best way to compare *str_a with
0xFF? (On my implementation, unadorned char is signed, and so I'm
using

if( *str_a == (signed char)0xFF ) ...

to quiet compiler warnings.)


It's not clear what exactly you want to achieve here. If you want to see
if the respective character value has a certain representation, the most
portable approach is to use a pointer to unsigned char:

if( *(unsigned char *)str_a == 0xFF ) ...

This works even if this pattern is a trap representation for the type
signed char.

OTOH, if you want to check that your character has a certain value,
simply compare against that value:

if( *str_a == -1 ) ...

Comparing an object with a value it cannot possibly take, as in your
example, doesn't make much sense a priori, so you have to explain your
exact intentions.

BTW, if str_a were an array of plain char, you had the following solution:

if( *str_a == '\xff' ) ...

but still not guaranteed to work if this bit pattern is a trap
representation for plain char.

As other people have already mentioned, (signed char)0xFF is useless for
your purpose, in a portability context, because the result need not be
the signed char value corresponding to that bit pattern. Casts really
are *conversion* operators and not devices for silencing the compilers.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #16
Richard Bos wrote:

Eric Sosman <Er*********@su n.com> wrote:
Christopher Benson-Manica wrote:
> (On my implementation, unadorned char is signed, and so I'm
> using
>
> if( *str_a == (signed char)0xFF ) ...


Undefined behavior, I think.


Implementation-defined, surely?


Harrumph. I guess so, but the distinction seems not
to be very important. When `(signed char)0xFF' is evaluated

"... either the result is implementation-defined or an
implementation-defined signal is raised." (6.3.1.3/3)

On the face of it, that's implementation-defined behavior and
not undefined behavior. But what if the implementation takes
the second alternative and raises a signal? If a function has
been installed to handle the signal

"If and when the function returns, if the value of _sig_
is [...] or any other implementation-defined value
corresponding to a computational exception, the behavior
is undefined; [...]" (7.14.1.1/3)

So if there's a handler, it cannot return without invoking
undefined behavior. I guess that means it must call abort()
or _Exit() or run an infinite loop; all of these have defined
effects, but are sufficiently unfortunate that they ought to
be avoided just about as strenuously as undefined behavior.
No nasal demons, surely, but no happy outcome either.

If there's not a handler, the implementation-defined signal
is treated as if one of SIG_IGN or SIG_DFL had been set up (the
choice is implementation-defined). If the handling is equivalent
to SIG_IGN, I think we're back in U.B. territory again: we're
told that we'll get *either* a result *or* a signal, not both.
Thus, we can't count on getting a result of any kind if a signal
is raised and ignored; the Standard doesn't specify any behavior,
so the behavior is undefined by omission (c.f. 3.4.3).

In the SIG_DFL case, the handling of the implementation-defined
signal is implementation-defined, not undefined. But right here
in the documentation I see

"The default handling for SIGBITROT causes demons
to fly out of your nose." (DS 9000 programmer's
manual, courtesy Armed Response Technologies)

.... which is not undefined behavior, but might seem so to a
casual observer. ;-)

Summary:

- You're right: `(signed char)0xFF' produces implementation-
defined, not undefined, behavior. My apologies.

- ... but since the I.B. is just about as unpredictable as
U.B., the programmer would be well-advised to avoid it.

- The *real* solution, I think, is to use `unsigned' types
whenever you want to deal with bits as bits. To ask the
question "Does this byte have all its bits set?", one
should not use potentially signed arithmetic. To answer
the question "Does this byte have the value 42?", either
signed or unsigned arithmetic will do.

- And, of course, all this is just another c.l.c exercise
in taking a census on a pinhead. We know perfectly well
that two's complement has won the game and extinguished
its competitors, right? And we're certain that it's the
ultimate in integer representations , and will never ever
be supplanted, right? Computer design is immune to the
vagaries of fashion, right?

(Ahem.) "Right?"

(I know you're out there; I can hear you breathing. C'mon,
stand up and be counted -- in two's complement ...)

--
Er*********@sun .com
Nov 14 '05 #17
In <40************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
Richard Bos wrote:

Eric Sosman <Er*********@su n.com> wrote:
> > Christopher Benson-Manica wrote:
> > > (On my implementation, unadorned char is signed, and so I'm
> > > using
> > >
> > > if( *str_a == (signed char)0xFF ) ...
>
> Undefined behavior, I think.


Implementation-defined, surely?


Harrumph. I guess so, but the distinction seems not
to be very important. When `(signed char)0xFF' is evaluated

"... either the result is implementation-defined or an
implementation-defined signal is raised." (6.3.1.3/3)

On the face of it, that's implementation-defined behavior and
not undefined behavior. But what if the implementation takes
the second alternative and raises a signal?


It won't, for backward compatibility with C89, which doesn't allow any
signal to be raised because of this. Breaking perfectly correct C89
code is not an option any serious implementor is going to adopt, *if* it
can be avoided.

This is a typical case where C99 fixed something that wasn't broken in
C89. And the person responsible for it couldn't produce a *convincing*
rationale...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #18

In article <ln************ @nuthaus.mib.or g>, Keith Thompson <ks***@mib.or g> writes:

In ASCII, all such characters happen to have values in the range
32..126. In EBCDIC, if I recall correctly, some basic characters have
codes greater than 127; I think this implies that in an implementation
that uses EBCDIC as its execution character set, type char must be
unsigned (assuming CHAR_BIT==8).


You recall correctly; the EBCDIC decimal digits, for example, are 0xF0
through 0xF9. It hadn't occurred to me earlier that this implied that
an EBCDIC implementation where CHAR_BIT==8 would have to make plain
char unsigned, but I suppose it would.

(I could check an EBCDIC implementation or two if anyone's curious, but
of course that wouldn't prove anything one way or the other.)

--
Michael Wojcik mi************@ microfocus.com

Viewers are bugs for famous brands.
-- unknown subtitler, Jackie Chan's _Thunderbolt_
Nov 14 '05 #19
Michael Wojcik <mw*****@newsgu y.com> wrote:

(I could check an EBCDIC implementation or two if anyone's curious, but
of course that wouldn't prove anything one way or the other.)


For what it's worth, every EBCDIC implementation I've ever seen -- and
I've seen a few -- has had plain char unsigned.

-Larry Jones

The problem with the future is that it keeps turning into the present.
-- Hobbes
Nov 14 '05 #20

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

Similar topics

19
6484
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
31513
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
4180
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) {
10
15666
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 */
22
5626
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
8
4068
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) {
11
2686
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int CountOccurrences(unsigned char const val, unsigned char const p,
7
5052
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
6
6462
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but vaguely . Can
0
9576
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
10074
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
9138
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
6847
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
5516
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...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
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.