473,395 Members | 1,763 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,395 software developers and data experts.

String Comparison

Consider:

printf("%d", "ABC" == "ABC");

The ANSI standard says that string comparsion using a comparison
operator (==) is not allowed. It recommends the use of the strcmp() in
<string.h>.

The Borland compiled programs print "0" on this; while the GCC and MS
VC++ compiled programs print "1".

Which compiler, would you say, returns the most appropriate answer,
not forgetting what the ANSI recommends...
Nov 14 '05 #1
8 1758
Dhruv Ahuja <dh********@gmail.com> scribbled the following:
Consider: printf("%d", "ABC" == "ABC"); The ANSI standard says that string comparsion using a comparison
operator (==) is not allowed. It recommends the use of the strcmp() in
<string.h>.
It certainly does not say it is not allowed. I think what it says is
that it will not necessarily yield the correct result based on the
strings' content.
The Borland compiled programs print "0" on this; while the GCC and MS
VC++ compiled programs print "1". Which compiler, would you say, returns the most appropriate answer,
not forgetting what the ANSI recommends...


All. None. Pick one. Or more. All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0. If they have the same content, it might return either 0
or 1, completely depending on the implementation.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #2

Dhruv Ahuja wrote:
Consider:

printf("%d", "ABC" == "ABC");

The ANSI standard says that string comparsion using a comparison
operator (==) is not allowed.
Please quote the standard where it says this.
It recommends the use of the strcmp() in
<string.h>.
Yeah, if you want to get an answer that means what you think it does.
The Borland compiled programs print "0" on this; while the GCC and MS
VC++ compiled programs print "1".
The comparison you show checks the equality of two pointers. If a
particular compiler has reused the string literal, then indeed the
pointers will compare because they will be the same one. If it
generates a unique string for each literal, then they won't.
Which compiler, would you say, returns the most appropriate answer,
not forgetting what the ANSI recommends...


As the standard doesn't recommend anything for the case you show,
either is appropriate.
Brian

Nov 14 '05 #3
On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0.


Only by coincidence, since the values of the pointers to the two will more
or less have be different. I guess its possible to concieve of an
implementation or mechanism where this would not be true.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #4
Mark McIntyre <ma**********@spamcop.net> scribbled the following:
On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0.
Only by coincidence, since the values of the pointers to the two will more
or less have be different. I guess its possible to concieve of an
implementation or mechanism where this would not be true.


If it is, *I'd* be glad to hear of it. Wouldn't this require memory
behaving differently depending on which pointer it's accessed through?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 14 '05 #5
On Mon, 01 Nov 2004 20:37:49 +0000, Joona I Palaste wrote:
Mark McIntyre <ma**********@spamcop.net> scribbled the following:
On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0.

Only by coincidence, since the values of the pointers to the two will more
or less have be different. I guess its possible to concieve of an
implementation or mechanism where this would not be true.


If it is, *I'd* be glad to hear of it. Wouldn't this require memory
behaving differently depending on which pointer it's accessed through?

"one\0two" and "one" might compare equal with some compilers/linkers.
Nov 14 '05 #6
In article <cm**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Mark McIntyre <ma**********@spamcop.net> scribbled the following:
On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0.

Only by coincidence, since the values of the pointers to the two will more
or less have be different. I guess its possible to concieve of an
implementation or mechanism where this would not be true.


If it is, *I'd* be glad to hear of it. Wouldn't this require memory
behaving differently depending on which pointer it's accessed through?


Not allowed to happen. If two pointers compare equal, following them
will give you the same object, which has to have the same value whichever
pointer you use to get at it.

Note that this only applies to equality conversions - it's possible to
have two different pointers (to nonidentical strings, f'rexample) such
that ptr1<ptr2 and ptr2<ptr1 both evaluate to false (which if pointers
were well-ordered would imply that they were equal).
(This can happen in a fully segmented memory model where only the offsets
into the segment are used for inequality comparisons, which is valid
since pointer inequality comparisons are only defined on pointers into
the same object or array. Comparing for equal or not-equal doesn't have
this constraint and would be required to check both segment and offset,
coming back with not-equal in this case.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Now, obviously I've picked a terrible example, ... But if I picked something
complicated like error-diffusion, it would be... well... complicated.
--Arthur J. O'Dwyer in comp.programming
Nov 14 '05 #7
In article <pa****************************@Utel.no>,
Nils O. Selåsdal <NO*@Utel.no> wrote:
"one\0two" and "one" might compare equal with some compilers/linkers.


Since we're talking about strings, which are defined as sequences of
non-'\0' characters terminated by a '\0' character, these are the same
string, so they're allowed to be pointed at by equal pointers.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Now, obviously I've picked a terrible example, ... But if I picked something
complicated like error-diffusion, it would be... well... complicated.
--Arthur J. O'Dwyer in comp.programming
Nov 14 '05 #8
On 1 Nov 2004 20:37:49 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
Mark McIntyre <ma**********@spamcop.net> scribbled the following:
On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0.

Only by coincidence, since the values of the pointers to the two will more
or less have be different. I guess its possible to concieve of an
implementation or mechanism where this would not be true.


If it is, *I'd* be glad to hear of it. Wouldn't this require memory
behaving differently depending on which pointer it's accessed through?


One trivial way to achieve this is to pass pointers between processes,
since the same logical address could map to different physical ones. This
is of course outside the realms of C.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #9

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

Similar topics

46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
5
by: MaSTeR | last post by:
Can anyone provide a practical short example of why in C# I shouldn't compare two strings with == ? If I write this in JAVA String string1 = "Widget"; if (string1 == "Widget") ...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
4
by: Jim Langston | last post by:
Is there any builtin lowercase std::string compare? Right now I'm doing this: if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 ) AmmoType = Item_Ammo_GunBullet; Is there anything the...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.