By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

about operation of unsigned type

P: n/a
Hello, everyone!

I find a version of strcpy(), I don't know why it return the unsigned
char value.
Can I change it into return *s1-*s2?

int strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == 0)
return 0;
s1++;
s2++;
}
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Steven wrote:
>
I find a version of strcpy(), I don't know why it return the
unsigned char value. Can I change it into return *s1-*s2?

int strcmp(const char *s1, const char *s2) {
.... snip code ...
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
You can't alter the functions in the standard library. Why do you
want to? Note that strcmp returns exactly what you programmed
above, without the possible overflows.

7.21.4.2 The strcmp function
Synopsis
[#1]
#include <string.h>
int strcmp(const char *s1, const char *s2);

Description

[#2] The strcmp function compares the string pointed to by
s1 to the string pointed to by s2.

Returns

[#3] The strcmp function returns an integer greater than,
equal to, or less than zero, accordingly as the string
pointed to by s1 is greater than, equal to, or less than the
string pointed to by s2.

and

7.21.2.3 The strcpy function
Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

Description

[#2] The strcpy function copies the string pointed to by s2
(including the terminating null character) into the array
pointed to by s1. If copying takes place between objects
that overlap, the behavior is undefined.

Returns

[#3] The strcpy function returns the value of s1.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #2

P: n/a
On Tue, 10 Jun 2008 22:32:28 -0700 (PDT), Steven
<mi********@hotmail.comwrote:
>Hello, everyone!

I find a version of strcpy(), I don't know why it return the unsigned
char value.
In discussing integer conversions, paragraph 6.3.1.8 says that if the
operands have the same type, no conversion is performed. Both
operands in your subtraction have type unsigned char. Therefore,
unsigned subtraction is performed. The result will always be
non-negative. This result is then converted to a non-negative int and
returned to the calling program.
>Can I change it into return *s1-*s2?
You cannot guarantee that this expression will not overflow.
>
int strcmp(const char *s1, const char *s2)
This function name belongs to the implementation. You are not allowed
to use it for yourself.
>{
while (*s1 == *s2)
{
if (*s1 == 0)
return 0;
s1++;
s2++;
}
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
Why not
return *s1 *s2 ? 1 : -1;
Remove del for email
Jun 27 '08 #3

P: n/a
On Jun 11, 12:39 pm, Barry Schwarz <schwa...@dqel.comwrote:
>
Why not
return *s1 *s2 ? 1 : -1;
Seems the most reasonable choice. We don't have the problem of
overflow over here.

Jun 27 '08 #4

P: n/a
On 11 Jun 2008 at 8:45, rahul wrote:
On Jun 11, 12:39 pm, Barry Schwarz <schwa...@dqel.comwrote:
>Why not
return *s1 *s2 ? 1 : -1;

Seems the most reasonable choice. We don't have the problem of
overflow over here.
On the other hand, you do have the problem that the compiler (depending
on its optimization capabilites) could well implement this using a
couple of jump operations instead of just a few moves and a single sub.
That's an issue in a standard library function that will be used a lot
by a lot of people.

Jun 27 '08 #5

P: n/a
Barry Schwarz wrote:
On Tue, 10 Jun 2008 22:32:28 -0700 (PDT), Steven
<mi********@hotmail.comwrote:
>Hello, everyone!

I find a version of strcpy(), I don't know why it return the unsigned
char value.

In discussing integer conversions, paragraph 6.3.1.8 says that if the
operands have the same type, no conversion is performed.
No, it says that if the *promoted* operands have the same
type, no conversion is performed.
Both
operands in your subtraction have type unsigned char. Therefore,
unsigned subtraction is performed.
No. `unsigned char' operands promote to `int' (on most
systems) or to `unsigned int' (if UCHAR_MAX INT_MAX), so
the implementation determines whether signed (usually) or
unsigned (sometimes) arithmetic is used.
The result will always be
non-negative. This result is then converted to a non-negative int and
returned to the calling program.
Your conclusion, then, is that the function never returns
a negative value? Have you tried it with `strcmp("0", "1")',
for example?

--
Er*********@sun.com
Jun 27 '08 #6

P: n/a
On Wed, 11 Jun 2008 11:30:43 -0400, Eric Sosman <Er*********@sun.com>
wrote:
>Barry Schwarz wrote:
>On Tue, 10 Jun 2008 22:32:28 -0700 (PDT), Steven
<mi********@hotmail.comwrote:
>>Hello, everyone!

I find a version of strcpy(), I don't know why it return the unsigned
char value.

In discussing integer conversions, paragraph 6.3.1.8 says that if the
operands have the same type, no conversion is performed.

No, it says that if the *promoted* operands have the same
type, no conversion is performed.
Both
operands in your subtraction have type unsigned char. Therefore,
unsigned subtraction is performed.

No. `unsigned char' operands promote to `int' (on most
systems) or to `unsigned int' (if UCHAR_MAX INT_MAX), so
the implementation determines whether signed (usually) or
unsigned (sometimes) arithmetic is used.
True, I obviously missed the lead-in paragraph in searching for the
conversion rules.

Which raises the question of why the OP believes that his function is
returning an unsigned value.
Remove del for email
Jun 27 '08 #7

P: n/a
Barry Schwarz wrote:
On Wed, 11 Jun 2008 11:30:43 -0400, Eric Sosman <Er*********@sun.com>
wrote:
>Barry Schwarz wrote:
>>On Tue, 10 Jun 2008 22:32:28 -0700 (PDT), Steven
<mi********@hotmail.comwrote:

Hello, everyone!

I find a version of strcpy(), I don't know why it return the unsigned
char value.
In discussing integer conversions, paragraph 6.3.1.8 says that if the
operands have the same type, no conversion is performed.
No, it says that if the *promoted* operands have the same
type, no conversion is performed.
>>Both
operands in your subtraction have type unsigned char. Therefore,
unsigned subtraction is performed.
No. `unsigned char' operands promote to `int' (on most
systems) or to `unsigned int' (if UCHAR_MAX INT_MAX), so
the implementation determines whether signed (usually) or
unsigned (sometimes) arithmetic is used.
True, I obviously missed the lead-in paragraph in searching for the
conversion rules.

Which raises the question of why the OP believes that his function is
returning an unsigned value.
Clearly it doesn't, and the O.P. is confused about the
"usual arithmetic conversions."

The function as shown is a work-alike for the standard
strcmp() on machines where unsigned char promotes to (signed)
int, and works *because* the promotion is to a signed type:
That's why the subtraction produces a negative value, as
required, when *s1 < *s2. Specifically, the function's

return *(unsigned const char *)s1
- *(unsigned const char *)(s2);

.... is evaluated as if it had been written

return (int)(*(unsigned const char*)s1)
- (int)(*(unsigned const char*)s2);

On those (rarer) machines where unsigned char promotes
to unsigned int, the function as shown is still all right in
the presence of a few additional guarantees, namely, that
conversion of a too-large unsigned int value to signed int
type doesn't trap or do anything weird, but silently produces
a negative value as is common on two's-complement machines.
On these machines the operation is equivalent to

return (int)(
(unsigned int)(*(unsigned const char*)s1)
- (unsigned int)(*(unsigned const char*)s2) );

.... and we rely on "helpful" behavior when out-of-range unsigned
values are converted to signed int.

On the (nonexistent?) machines where unsigned char promotes
to unsigned int and where the conversion of large unsigned int
values to signed int does Weird Stuff, the function as shown
would not work: The Weird Stuff would occur whenever the s1
string was less than the s2 string. On such a system you'd
need something like one of the conditionals shown elsethread.

--
Er*********@sun.com

Jun 27 '08 #8

P: n/a
Thank you for your reply!
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.