473,473 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string comparison: signed or unsigned char?

g
Hi all.
Maybe this question has been asked many times before, but I was not able to find
any pointer. I apologize in advance, for it refers to a particular standard
library implementation (GNU C++ version 3.x), but perhaps it's more general than
that.

The following program (to be compiled with g++ 3.x, 2.9x is not enough)

#include <iostream>
#include <string>
#define BLURB(x) #x << "\t== " << (x) << '\n'
int main()
{
std::basic_string<char> s1, s2;
s1 = 0xe0;
s2 = 'a';
std::cout << "s1 == '" << s1 << "'\ns2 == '" << s2 << "'\n";
std::cout << BLURB( (s1 < s2) );
std::cout << BLURB( (s1[0] < s2[0]) );
std::cout << BLURB( std::char_traits<char>::lt(s1[0], s2[0]) );
std::cout << BLURB( std::char_traits<char>::compare(s1.c_str(),
s2.c_str(), 1) );
std::cout << BLURB( s1.compare(s2) );
return 0;
}

produces this output on my x86 linux pc:

s1 == 'à'
s2 == 'a'
(s1 < s2) == 0
(s1[0] < s2[0]) == 1
std::char_traits<char>::lt(s1[0], s2[0]) == 1
std::char_traits<char>::compare(s1.c_str(), s2.c_str(), 1) == 1
s1.compare(s2) == 1

The above results are counter-intuitive, but agree with the
behaviour of C standard library (1999 standard):
- strcmp and memcmp treat their arguments as unsigned char*, so
that strcmp(s1.c_str(), s2.c_str()) > 0 (meaning s1 > s2)
- on my platform char is signed, so s1[0] < s2[0] (because s1[0] < 0)

On the other hand, Stroustrup's TC++PL, section 20.2.1 "Character traits"
reports "The compare() function uses lt() and eq() to compare characters.",
so I'd expect
std::char_traits<char>::lt(s1[0], s2[0])
and
std::char_traits<char>::compare(s1.c_str(), s2.c_str(), 1)
to return consistent results, which they do not. As a side effect,
s1.compare(s2) is not consistent with std::char_traits<char>::lt()
either.

As far as I can see, GNU libstdc++5 implementation of
std::char_traits<char>::compare() uses memcmp() instead of lt(), and so
inherits the unsigned char comparison, while std::char_traits<char>::lt()
plainly uses '<' to compare its arguments, keeping them signed.

It's quite likely that I am missing something in Stroustrup's book.
Does anybody know what the standard mandates?

giuseppe
Jul 22 '05 #1
0 2897

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

Similar topics

6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
9
by: c_beginner | last post by:
Dear group, I want to implement a solution to the following link: http://acmicpc-live-archive.uva.es/nuevoportal/data/p2006.pdf As a beginning I am trying to implement this little sample...
2
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
6
by: compcreator | last post by:
I have tried the following program. The problem is it is printing False I checked values for a and b but there is something wrong with the comparison. I thing it might be because of signed and...
9
by: junky_fellow | last post by:
guys, I need to declare a pointer to a string. Which of the following is better and why? char *pStr; unsigned char *pStr; thanks for any help ...
15
by: Cartoper | last post by:
There is one little static C library to manage the serial number and unlock key for my application. Today it is compiled with Microsoft VC6 and linked into both VC6 modules and VS2005 modules. It...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
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
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...
1
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
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,...
1
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...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.