Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Is there a shorter solution ? |
| Date | 2011-08-10 11:49 -0700 |
| Organization | None to speak of |
| Message-ID | <ln8vr19k54.fsf@nuthaus.mib.org> (permalink) |
| References | <f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com> <27445c82-9386-477b-a686-09658ce156f8@n35g2000yqf.googlegroups.com> <2e70f5da-2af5-4a9c-a6f7-047288cf670d@y8g2000prd.googlegroups.com> <j1udcm$p6l$1@dont-email.me> |
James Kuyper <jameskuyper@verizon.net> writes:
> On 08/10/2011 12:51 PM, HumbleWorker wrote:
>> On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote:
>>> On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
>>>
>>> int main()
>>> {
>>> char *k = "abababababawhatever";
>>> while(*k) numA += *k++ == 'a';
>>> printf("Number of a's = %d\n", numA);
>>> return 0;
>>>
>>> }
>>>
>>> :-)
>>
>> Thanks ! Is it guaranteed that a logical true returns 1 ?
>
> All logical expressions in C indicate truth by having a result of 1.
For certain meanings of the phrase "logical expressions".
The <, <=, >, >=, ==, !=, !, &&, and || operators are all defined to
yield either 0 or 1. I believe that's an exhaustive list.
On the other hand, of course, a scalar expression used as a condition is
treated as false if it compares equal to 0, true otherwise.
The is*() functions declared in <ctype.h> are only defined to return 0
for false, and some unspecified non-zero value for true. The same can
potentially apply to any expression used as a condition, unless its
top-level operator is one of the 9 listed above *or* it's specifically
written to yield only 0 or 1.
So:
if (x < y) count ++; // ok
count += (x < y); // ok and equivalent to the above
if (isdigit(c)) count ++; // ok
count += !!isdigit(c); // ok and equivalent to the above
count += isdigit(c); // legal but *not* equivalent to the above
#define FALSE 0
#define TRUE 1 // useful if you don't have <stdbool.h>
int cond = isdigit(c);
if (cond == FALSE) ...; // works, but unnecessarily verbose
if (cond == TRUE) ...; // dangerous, fails if cond < 0 or cond > 1
if (cond) ...; // the best way
See also section section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there a shorter solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-08-06 10:25 -0700
Re: Is there a shorter solution ? Ike Naar <ike@sdf.lonestar.org> - 2011-08-06 17:42 +0000
Re: Is there a shorter solution ? John Gordon <gordon@panix.com> - 2011-08-06 17:44 +0000
Re: Is there a shorter solution ? Kenneth Brody <kenbrody@spamcop.net> - 2011-08-08 09:47 -0400
Re: Is there a shorter solution ? Keith Thompson <kst-u@mib.org> - 2011-08-08 08:09 -0700
Re: Is there a shorter solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-08-10 07:20 -0700
Re: Is there a shorter solution ? tom st denis <tom@iahu.ca> - 2011-08-10 09:43 -0700
Re: Is there a shorter solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-08-10 09:51 -0700
Re: Is there a shorter solution ? James Kuyper <jameskuyper@verizon.net> - 2011-08-10 12:57 -0400
Re: Is there a shorter solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-08-10 10:13 -0700
Re: Is there a shorter solution ? blp@cs.stanford.edu (Ben Pfaff) - 2011-08-10 10:17 -0700
Re: Is there a shorter solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-08-10 10:29 -0700
Re: Is there a shorter solution ? tom st denis <tom@iahu.ca> - 2011-08-10 11:29 -0700
Re: Is there a shorter solution ? "J. J. Farrell" <jjf@bcs.org.uk> - 2011-08-11 03:02 +0100
Re: Is there a shorter solution ? Kenneth Brody <kenbrody@spamcop.net> - 2011-08-12 11:39 -0400
Re: Is there a shorter solution ? Keith Thompson <kst-u@mib.org> - 2011-08-12 09:06 -0700
Re: Is there a shorter solution ? James Kuyper <jameskuyper@verizon.net> - 2011-08-10 14:00 -0400
Re: Is there a shorter solution ? Seebs <usenet-nospam@seebs.net> - 2011-08-11 01:46 +0000
Re: Is there a shorter solution ? Keith Thompson <kst-u@mib.org> - 2011-08-10 11:49 -0700
Re: Is there a shorter solution ? James Kuyper <jameskuyper@verizon.net> - 2011-08-10 15:03 -0400
Re: Is there a shorter solution ? Keith Thompson <kst-u@mib.org> - 2011-08-10 13:23 -0700
Re: Is there a shorter solution ? gordonb.d3r7p@burditt.org (Gordon Burditt) - 2011-08-10 18:15 -0500
Re: Is there a shorter solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-08-10 11:26 -0700
Re: Is there a shorter solution ? James Kuyper <jameskuyper@verizon.net> - 2011-08-10 15:14 -0400
csiph-web