473,624 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2329
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********@hot mail.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********@hot mail.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*********@su n.com>
wrote:
>Barry Schwarz wrote:
>On Tue, 10 Jun 2008 22:32:28 -0700 (PDT), Steven
<mi********@ho tmail.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*********@su n.com>
wrote:
>Barry Schwarz wrote:
>>On Tue, 10 Jun 2008 22:32:28 -0700 (PDT), Steven
<mi********@h otmail.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)(*(unsigne d const char*)s1)
- (int)(*(unsigne d 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
1286
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 class defined like this: template<unsigned p> class GF{ public:
8
2236
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. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
9
2756
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
26489
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
3124
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 help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
10
2400
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
11
1897
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
1732
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 (s!=0) { printf ("%d", *s); s++;
160
5589
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
8234
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
8677
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
7158
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...
1
6110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
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
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
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
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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.