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

about operation of unsigned type

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
8 2312
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
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
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
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
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
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
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
Thank you for your reply!
Jun 27 '08 #9

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

Similar topics

3
by: Prasanna | last post by:
Hi, I am enountering this problem when compiling my code. error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'const class whatever' I have the template...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
10
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
11
by: sethukr | last post by:
hi everybody, char str="string"; str^=str; can anybody plz tell me the use of ^operator in string??? -Sethu
5
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.