Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #9045 > unrolled thread
| Started by | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| First post | 2011-08-06 10:25 -0700 |
| Last post | 2011-08-10 15:14 -0400 |
| Articles | 20 on this page of 24 — 11 participants |
Back to article view | Back to comp.lang.c
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
Page 1 of 2 [1] 2 Next page →
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-08-06 10:25 -0700 |
| Subject | Is there a shorter solution ? |
| Message-ID | <f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com> |
Count the number of a in cX below
#include <stdio.h>
int main()
{
char cX[] = "ababcabcdabcaba", * k = cX;
int numA = 0;
while (*k && ('a' == *k++ ? ++numA : 1));
printf ("Number of a's = %u\n", numA);
return 0;
}
[toc] | [next] | [standalone]
| From | Ike Naar <ike@sdf.lonestar.org> |
|---|---|
| Date | 2011-08-06 17:42 +0000 |
| Message-ID | <slrn3vfsj3qv8q.234.ike@sdf.lonestar.org> |
| In reply to | #9045 |
On 2011-08-06, HumbleWorker <amardeep.developer@gmail.com> wrote:
> Count the number of a in cX below
>
> #include <stdio.h>
>
> int main()
> {
> char cX[] = "ababcabcdabcaba", * k = cX;
> int numA = 0;
>
> while (*k && ('a' == *k++ ? ++numA : 1));
>
> printf ("Number of a's = %u\n", numA);
The ``%u'' format expects an unsigned int, you're supplying a signed int.
Don't do that.
Either use ``int numA'' with ``%d'', or ``unsigned int numA'' with ``%u''.
>
> return 0;
> }
--
ike@sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lonestar.org
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-06 17:44 +0000 |
| Message-ID | <j1juhc$sn7$1@reader1.panix.com> |
| In reply to | #9045 |
In <f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com> HumbleWorker <amardeep.developer@gmail.com> writes:
> Count the number of a in cX below
Did you really need to post this again after just five minutes?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2011-08-08 09:47 -0400 |
| Message-ID | <c7KdnXnPk7FqdaLTnZ2dnUVZ_gCdnZ2d@bestweb.net> |
| In reply to | #9049 |
On 8/6/2011 1:44 PM, John Gordon wrote: > In<f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com> HumbleWorker<amardeep.developer@gmail.com> writes: > >> Count the number of a in cX below > > Did you really need to post this again after just five minutes? Because he posted from Google Groups, thinks this is a "chat room", and figured the only reason he didn't get a reply within five minutes is because he didn't post it correctly the first time? -- Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-08 08:09 -0700 |
| Message-ID | <lnk4aoaqje.fsf@nuthaus.mib.org> |
| In reply to | #9075 |
Kenneth Brody <kenbrody@spamcop.net> writes:
> On 8/6/2011 1:44 PM, John Gordon wrote:
>> In<f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com>
>> HumbleWorker<amardeep.developer@gmail.com> writes:
>>
>>> Count the number of a in cX below
>>
>> Did you really need to post this again after just five minutes?
>
> Because he posted from Google Groups, thinks this is a "chat
> room", and figured the only reason he didn't get a reply within
> five minutes is because he didn't post it correctly the first time?
And apparently the Google Groups Usenet interface has been having
problems. See the recent announcements in news.announce.important.
(But that doesn't particularly explain two posts in five minutes.)
--
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"
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-08-10 07:20 -0700 |
| Message-ID | <a8276370-80e6-46d9-8477-3232b0619e7e@h25g2000prf.googlegroups.com> |
| In reply to | #9079 |
On Aug 8, 8:09 pm, Keith Thompson <ks...@mib.org> wrote: > Kenneth Brody <kenbr...@spamcop.net> writes: > > On 8/6/2011 1:44 PM, John Gordon wrote: >And apparently the Google Groups Usenet interface has been having >problems. See the recent announcements in news.announce.important. Yes this was the reason. I could'nt see the listing immediately, so thought something was wrong at my end. By the way, why are people worring about two posts ? Have we stopped discussing C here ?
[toc] | [prev] | [next] | [standalone]
| From | tom st denis <tom@iahu.ca> |
|---|---|
| Date | 2011-08-10 09:43 -0700 |
| Message-ID | <27445c82-9386-477b-a686-09658ce156f8@n35g2000yqf.googlegroups.com> |
| In reply to | #9045 |
On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
> Count the number of a in cX below
>
> #include <stdio.h>
>
> int main()
> {
> char cX[] = "ababcabcdabcaba", * k = cX;
> int numA = 0;
> while (*k && ('a' == *k++ ? ++numA : 1));
> printf ("Number of a's = %u\n", numA);
> return 0;
> }
Um shorter eh ...
int numA;
int main()
{
char *k = "abababababawhatever";
while(*k) numA += *k++ == 'a';
printf("Number of a's = %d\n", numA);
return 0;
}
:-)
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-08-10 09:51 -0700 |
| Message-ID | <2e70f5da-2af5-4a9c-a6f7-047288cf670d@y8g2000prd.googlegroups.com> |
| In reply to | #9152 |
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 ?
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-10 12:57 -0400 |
| Message-ID | <j1udcm$p6l$1@dont-email.me> |
| In reply to | #9153 |
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.
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-08-10 10:13 -0700 |
| Message-ID | <1932f584-f7d2-4cfe-84ae-2bb4cac93521@a2g2000prf.googlegroups.com> |
| In reply to | #9154 |
On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> 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.
As we have know that any non-zero int evaluates to logical true, so
vice-versa doesn't a compiler have the freedom to return anything non-
zero ? Is there some standard that enforces that it should be 1 only ?
[toc] | [prev] | [next] | [standalone]
| From | blp@cs.stanford.edu (Ben Pfaff) |
|---|---|
| Date | 2011-08-10 10:17 -0700 |
| Message-ID | <87k4alnq2p.fsf@blp.benpfaff.org> |
| In reply to | #9158 |
HumbleWorker <amardeep.developer@gmail.com> writes:
> On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
>> 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.
>
> As we have know that any non-zero int evaluates to logical true, so
> vice-versa doesn't a compiler have the freedom to return anything non-
> zero ? Is there some standard that enforces that it should be 1 only ?
Yes, the C standard says so, e.g.:
6.5.13 Logical AND operator
The && operator shall yield 1 if both of its operands
compare unequal to 0; otherwise, it yields 0. The result has
type int.
...
6.5.14 Logical OR operator
The || operator shall yield 1 if either of its operands
compare unequal to 0; otherwise, it yields 0. The result has
type int.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-08-10 10:29 -0700 |
| Message-ID | <6251312f-191f-433c-b6d9-18d1354b8fcd@u12g2000prc.googlegroups.com> |
| In reply to | #9160 |
On Aug 10, 10:17 pm, b...@cs.stanford.edu (Ben Pfaff) wrote:
> HumbleWorker <amardeep.develo...@gmail.com> writes:
> > On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> >> 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.
>
> > As we have know that any non-zero int evaluates to logical true, so
> > vice-versa doesn't a compiler have the freedom to return anything non-
> > zero ? Is there some standard that enforces that it should be 1 only ?
>
> Yes, the C standard says so, e.g.:
>
> 6.5.13 Logical AND operator
>
> The && operator shall yield 1 if both of its operands
> compare unequal to 0; otherwise, it yields 0. The result has
> type int.
>
> ...
>
That's nice. I learnt a new thing today !
[toc] | [prev] | [next] | [standalone]
| From | tom st denis <tom@iahu.ca> |
|---|---|
| Date | 2011-08-10 11:29 -0700 |
| Message-ID | <e3d6ff01-e76e-4d5c-bbf5-83a48dccb74c@b34g2000yqi.googlegroups.com> |
| In reply to | #9162 |
On Aug 10, 1:29 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
> On Aug 10, 10:17 pm, b...@cs.stanford.edu (Ben Pfaff) wrote:
>
>
>
>
>
>
>
> > HumbleWorker <amardeep.develo...@gmail.com> writes:
> > > On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> > >> 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.
>
> > > As we have know that any non-zero int evaluates to logical true, so
> > > vice-versa doesn't a compiler have the freedom to return anything non-
> > > zero ? Is there some standard that enforces that it should be 1 only ?
>
> > Yes, the C standard says so, e.g.:
>
> > 6.5.13 Logical AND operator
>
> > The && operator shall yield 1 if both of its operands
> > compare unequal to 0; otherwise, it yields 0. The result has
> > type int.
>
> > ...
>
> That's nice. I learnt a new thing today !
That being said, if you wrote code like "a += b == 4;" for a project I
was working on I might have words with you. But yes, they eval to 1
or 0.
The ideal way to write that is something like
if (b == 4) ++a;
or if you must ...
a += (b == 4) ? 1 : 0;
Though it's redundant it's a heck of a lot quicker to read... I
suppose
a += (b == 4);
With the brackets is ok to read ... still not my preferred style and
most don't write like that either.
Tom
[toc] | [prev] | [next] | [standalone]
| From | "J. J. Farrell" <jjf@bcs.org.uk> |
|---|---|
| Date | 2011-08-11 03:02 +0100 |
| Message-ID | <j1vd6r$hs8$1@dont-email.me> |
| In reply to | #9166 |
tom st denis wrote: > On Aug 10, 1:29 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote: >>>>>> On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote: >>>>>>> >>>>>>> while(*k) numA += *k++ == 'a'; > > That being said, if you wrote code like "a += b == 4;" for a project I > was working on I might have words with you. But yes, they eval to 1 > or 0. > > The ideal way to write that is something like > > if (b == 4) ++a; > > or if you must ... > > a += (b == 4) ? 1 : 0; > > Though it's redundant it's a heck of a lot quicker to read... Quicker to read? The exact opposite for me - this one is about the worst of all the possibilities you give. I find the one below much clearer and quicker and easier to read reliably than the one above. When looking at the above I'd tend to assume the conditional expression was doing something useful, requiring me to read it carefully to be sure of what it did - and then check again even more carefully when the first reading said it didn't do anything useful. > I suppose > > a += (b == 4); > > With the brackets is ok to read ... still not my preferred style and > most don't write like that either. > > Tom
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2011-08-12 11:39 -0400 |
| Message-ID | <NrCdneCYgKO81NjTnZ2dnUVZ_sudnZ2d@bestweb.net> |
| In reply to | #9160 |
On 8/10/2011 1:17 PM, Ben Pfaff wrote: [...] > Yes, the C standard says so, e.g.: > > 6.5.13 Logical AND operator > > The&& operator shall yield 1 if both of its operands > compare unequal to 0; otherwise, it yields 0. The result has > type int. > > .... > > 6.5.14 Logical OR operator > > The || operator shall yield 1 if either of its operands > compare unequal to 0; otherwise, it yields 0. The result has > type int. Did K&R say anything about this? I seem to recall using a compiler in K&R1 days which used -1 rather than 1. (But that was many years ago, and I may be misremembering things.) -- Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-12 09:06 -0700 |
| Message-ID | <lnei0q7gxo.fsf@nuthaus.mib.org> |
| In reply to | #9260 |
Kenneth Brody <kenbrody@spamcop.net> writes:
> On 8/10/2011 1:17 PM, Ben Pfaff wrote:
> [...]
>> Yes, the C standard says so, e.g.:
>>
>> 6.5.13 Logical AND operator
>>
>> The&& operator shall yield 1 if both of its operands
>> compare unequal to 0; otherwise, it yields 0. The result has
>> type int.
>>
>> ....
>>
>> 6.5.14 Logical OR operator
>>
>> The || operator shall yield 1 if either of its operands
>> compare unequal to 0; otherwise, it yields 0. The result has
>> type int.
>
> Did K&R say anything about this? I seem to recall using a compiler in K&R1
> days which used -1 rather than 1. (But that was many years ago, and I may
> be misremembering things.)
<http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf> is a C reference manual
from 1975, 3 years before K&R1. 7.12 says:
The || operator returns 1 if either of its operands is nonzero, and
0 otherwise.
<http://cm.bell-labs.com/cm/cs/who/dmr/cman74.pdf>, from Jan 1974, says
the same thing (it's a scan of a printout).
--
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"
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-10 14:00 -0400 |
| Message-ID | <j1uh3u$n26$1@dont-email.me> |
| In reply to | #9158 |
On 08/10/2011 01:13 PM, HumbleWorker wrote:
> On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
>> 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.
>
> As we have know that any non-zero int evaluates to logical true, so
> vice-versa doesn't a compiler have the freedom to return anything non-
> zero ? Is there some standard that enforces that it should be 1 only ?
It's not just "some" standard, it's specifically the C standard (ISO/IEC
9899:1999). There's no enforcement, just a mandate. It does indeed
mandate that the result is 1 for each logical expression. Citations:
6.5.3p5: !
6.5.8p6: < > <= >=
6.5.9p3: == !=
6.5.13p3: &&
6.5.14p3: ||
As shown above, this mandate allows logical expressions to be used in
numerical contexts in ways that could not be guaranteed to work if the
standard gave implementations more freedom in this matter.
Another common use of this feature is in the context of the comparison
functions used by the qsort() and bsearch() standard library functions;
when comparing ordinary numerical types, the body of that function can
be written quite simply as
return (*left > *right) - (*left < *right);
It's debatable whether this feature was a great idea; but it's been a
part of C from the very beginning, and a lot of existing code relies
upon this feature.
[toc] | [prev] | [next] | [standalone]
| From | Seebs <usenet-nospam@seebs.net> |
|---|---|
| Date | 2011-08-11 01:46 +0000 |
| Message-ID | <slrnj46d0v.1oh9.usenet-nospam@guild.seebs.net> |
| In reply to | #9158 |
On 2011-08-10, HumbleWorker <amardeep.developer@gmail.com> wrote: > As we have know that any non-zero int evaluates to logical true, so > vice-versa doesn't a compiler have the freedom to return anything non- > zero? No. > Is there some standard that enforces that it should be 1 only ? Yes. It is called the language specification. The logical operators are defined as yielding 0 or 1. -s -- Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-10 11:49 -0700 |
| Message-ID | <ln8vr19k54.fsf@nuthaus.mib.org> |
| In reply to | #9154 |
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"
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-10 15:03 -0400 |
| Message-ID | <j1ukq2$hr2$1@dont-email.me> |
| In reply to | #9168 |
On 08/10/2011 02:49 PM, Keith Thompson wrote: > James Kuyper <jameskuyper@verizon.net> writes: ... >> 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. That's the meaning that I intended for "logical expressions". > 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. And that is not. I'd refer to those as conditions, not logical expressions. It's a distinction between what the value of an expression means (0: false, 1: true) and how the value of the expression is used (0: false, non-0: true). In a very different but somewhat C-like language, it would be philosophically cleaner to have logical expressions give results of boolean type; conditions constrained to take values of boolean type; and no conversions (neither implicit nor explicit) to or from the boolean type. The equivalent of C's implicit conversions could be done by non-boolean_value != 0 boolean_value ? 1 : 0 It would break way too much existing code to make any such change to C itself, of course.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web