🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #236

Re: compare literals / if( "cat"== "dog") {}

#236
From: Lawrence D’Oliveiro
Date: Sun, 11 Oct 2026 21:05
28 lines
2398 bytes
On Sun, 11 Oct 2026 18:28:51 GMT, MitchAlsup wrote:

> "cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory;
> so, if you want character comparison you want:
>
>      if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
>
> And that one will never have the flow of control enter the then
> clause; whereas:
>
>      if( strcmp( "cat", "cat" ) == 0 ) //cat = cat
>
> and you will always enter the then clause, regardless of whether the
> compiler uses the same literal twice or not.

In C++ and Python, you can overload the “==” operator to define
equality as whatever makes sense for your custom object type. In C,
you can’t.

Java tried to copy the good parts from C++, but unfortunately one of
the things they left out was custom operator overloads. So that
suffers from the same problem with string comparisons as C does. Their
workaround is to introduce the concept of “interned” strings. But
then, there is no protection against accidentally mixing interned and
non-interned strings, leading to comparisons that work the way you
expect one time, but unexpectedly fail another time.

(Did someone say “worst of both worlds”?)
Technical Details
Message-ID: <11agtnl$30k5e$2@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11ae962$2365v$1@dont-email.me> <1791743331-5857@newsgrouper.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D’Oliveiro <ldo@nz.invalid>
Newsgroups: comp.lang.c
Subject: Re: compare literals / if( "cat"== "dog") {}
Date: Sun, 11 Oct 2026 21:05:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <11agtnl$30k5e$2@dont-email.me>
References: <11ae962$2365v$1@dont-email.me> <1791743331-5857@newsgrouper.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 21:05:58 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3166382"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18Eva//bQPHPfZgg4+Iv5ek"; posting-host="a82cbb4a9e343247fd3ed93594ed57ba"
User-Agent: Pan/0.165 (Kostiantynivka)
Cancel-Lock: sha1:xABPwvWf6B2QxFdx56ti4ZCa8Ko= sha256:WNVMzl2ifFmxtr1dfrobZT1tIMm5DYYhDckXgnSMyBA=
	sha1:/iNRFY60+vwI7CsIZtSSCCVk1Uo= sha256:eIMiLPkTYxwAFMp2TPnZdTmXinD8VAMqkSiiDeF8CgI=