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 | 4 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 2 of 2 — ← Prev page 1 [2]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-10 13:23 -0700 |
| Message-ID | <ln4o1p9fsp.fsf@nuthaus.mib.org> |
| In reply to | #9169 |
James Kuyper <jameskuyper@verizon.net> writes:
> 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".
I assumed so, but it's not clear from the phrase itself just what it
means; someone else might easily think of ``isdigit(c)'' as a logical
expression. (The standard doesn't define the term.)
>> 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).
That's a useful distinction, and it would be nice to have universally
agreed terms make that distinction.
> 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.
Yes, and there are plenty of languages like that, though most of them
aren't particularly C-like (Pascal, Ada). To avoid triggering a
language war, I'll just mention that I do personally prefer one approach
over the other without saying which.
--
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 | gordonb.d3r7p@burditt.org (Gordon Burditt) |
|---|---|
| Date | 2011-08-10 18:15 -0500 |
| Message-ID | <MLGdnUzOzbKbjN7TnZ2dnUVZ_oGdnZ2d@posted.internetamerica> |
| In reply to | #9153 |
> Thanks ! Is it guaranteed that a logical true returns 1 ? Yes, for only some things that return "logical true". Operators such as &&, ||, <, >, ==, !=, <=, >=, and ! are guaranteed to return 0 or 1. (The comma operator is not included, although I use commas to separate operators.) I do not guarantee that this is a complete list. Bitwise operators such as &, |, ^, and ~ do not return "logical true", they return a number often viewed as a bit pattern, although an expression like (flags & 0x0020) is sometimes used as a "boolean". The standard functions of the form is*() declared in <ctype.h> or <wctype.h>, which you'd think return a "boolean" value, such as isalpha() and isdigit(), return 0 or non-zero, *NOT* 0 or 1. (C doesn't really have a "boolean" type. C99 "bool" is really a thinly disguised integer.) The same (0 or non-zero) applies to feof(), ferror(), isfinite(), isinf(), isnan(), isnormal(), isgreater(), isgreaterequal(), isless(), islessequal(), islessgreater(), and isunordered(). I have probably left out some.
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-08-10 11:26 -0700 |
| Message-ID | <0980e8fe-58a7-413c-ae8a-55a7ca4fb3ca@u12g2000prc.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:
>
> > 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;
>
> }
>
> :-)
I am curious about one more thing. What would be the considerations of
efficiency when we use a for loop like this instead of the while loop
above ?
int main()
{
int numA = 0;
char* k = "abababababawhatever";
for (; *k; numA += *k++ == 'a');
printf("Number of a's = %d\n", numA);
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-10 15:14 -0400 |
| Message-ID | <4E42D8AF.7000508@verizon.net> |
| In reply to | #9165 |
On 08/10/2011 02:26 PM, HumbleWorker wrote:
> On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote:
...
>> int numA;
>> int main()
>> {
>> char *k = "abababababawhatever";
>> while(*k) numA += *k++ == 'a';
>> printf("Number of a's = %d\n", numA);
>> return 0;
>>
>> }
>>
>> :-)
>
> I am curious about one more thing. What would be the considerations of
> efficiency when we use a for loop like this instead of the while loop
> above ?
>
> int main()
> {
> int numA = 0;
> char* k = "abababababawhatever";
> for (; *k; numA += *k++ == 'a');
> printf("Number of a's = %d\n", numA);
> return 0;
> }
The while-for conversion has no efficiency implications. Those parts of
the two programs are precisely equivalent, and if you're using a
compiler that isn't smart enough to recognize that fact and optimize
them the same way, you've got much bigger problems than simple
optimization to worry about.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web