Article View: comp.lang.c
Article #228Re: compare literals / if( "cat"== "dog") {}
From: MitchAlsup
Date: Sun, 11 Oct 2026 18:28
Date: Sun, 11 Oct 2026 18:28
20 lines
1402 bytes
1402 bytes
fir <profesor.fir@gmail.com> posted:
-------------------
>
> if("cat" == "cat") will return true and
> if("cat" == "dog") will return false
"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.
Technical Details
Message-ID:
<1791743331-5857@newsgrouper.org>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!newsgrouper.org!.POSTED!not-for-mailReferences:
<11ae962$2365v$1@dont-email.me>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!newsgrouper.org!.POSTED!not-for-mail
From: MitchAlsup <user5857@newsgrouper.org.invalid>
Newsgroups: comp.lang.c
Subject: Re: compare literals / if( "cat"== "dog") {}
References: <11ae962$2365v$1@dont-email.me>
Date: Sun, 11 Oct 2026 18:28:51 GMT
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <1791743331-5857@newsgrouper.org>
Injection-Info: newsgrouper.org; mail-complaints-to="newsgrouper@yahoo.com"; posting-account=user5857
Injection-Date: Sun, 11 Oct 2026 18:28:51 GMT
User-Agent: Newsgrouper/0.9.0