Is there a shorter solution ?

Discussion in 'C Programming' started by HumbleWorker, Aug 6, 2011.

  1. HumbleWorker

    HumbleWorker Guest

    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;
    }
     
    HumbleWorker, Aug 6, 2011
    #1
    1. Advertising

  2. HumbleWorker

    Ike Naar Guest

    On 2011-08-06, HumbleWorker <> 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;
    > }



    --

    SDF Public Access UNIX System - http://sdf.lonestar.org
     
    Ike Naar, Aug 6, 2011
    #2
    1. Advertising

  3. HumbleWorker

    John Gordon Guest

    In <> HumbleWorker <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Aug 6, 2011
    #3
  4. Kenneth Brody <> writes:
    > On 8/6/2011 1:44 PM, John Gordon wrote:
    >> In<>
    >> HumbleWorker<> 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) <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"
     
    Keith Thompson, Aug 8, 2011
    #4
  5. HumbleWorker

    HumbleWorker Guest

    On Aug 8, 8:09 pm, Keith Thompson <> wrote:
    > Kenneth Brody <> 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 ?
     
    HumbleWorker, Aug 10, 2011
    #5
  6. HumbleWorker

    tom st denis Guest

    On Aug 6, 1:25 pm, HumbleWorker <> 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;
    }

    :)
     
    tom st denis, Aug 10, 2011
    #6
  7. HumbleWorker

    HumbleWorker Guest

    On Aug 10, 9:43 pm, tom st denis <> wrote:
    > On Aug 6, 1:25 pm, HumbleWorker <> 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 ?
     
    HumbleWorker, Aug 10, 2011
    #7
  8. HumbleWorker

    James Kuyper Guest

    On 08/10/2011 12:51 PM, HumbleWorker wrote:
    > On Aug 10, 9:43 pm, tom st denis <> wrote:
    >> On Aug 6, 1:25 pm, HumbleWorker <> 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.
     
    James Kuyper, Aug 10, 2011
    #8
  9. HumbleWorker

    HumbleWorker Guest

    On Aug 10, 9:57 pm, James Kuyper <> wrote:
    > On 08/10/2011 12:51 PM, HumbleWorker wrote:
    >
    > > On Aug 10, 9:43 pm, tom st denis <> wrote:
    > >> On Aug 6, 1:25 pm, HumbleWorker <> 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 ?
     
    HumbleWorker, Aug 10, 2011
    #9
  10. HumbleWorker

    Ben Pfaff Guest

    HumbleWorker <> writes:

    > On Aug 10, 9:57 pm, James Kuyper <> wrote:
    >> On 08/10/2011 12:51 PM, HumbleWorker wrote:
    >>
    >> > On Aug 10, 9:43 pm, tom st denis <> wrote:
    >> >> On Aug 6, 1:25 pm, HumbleWorker <> 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;}}}
     
    Ben Pfaff, Aug 10, 2011
    #10
  11. HumbleWorker

    HumbleWorker Guest

    On Aug 10, 10:17 pm, (Ben Pfaff) wrote:
    > HumbleWorker <> writes:
    > > On Aug 10, 9:57 pm, James Kuyper <> wrote:
    > >> On 08/10/2011 12:51 PM, HumbleWorker wrote:

    >
    > >> > On Aug 10, 9:43 pm, tom st denis <> wrote:
    > >> >> On Aug 6, 1:25 pm, HumbleWorker <> 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 !
     
    HumbleWorker, Aug 10, 2011
    #11
  12. HumbleWorker

    James Kuyper Guest

    On 08/10/2011 01:13 PM, HumbleWorker wrote:
    > On Aug 10, 9:57 pm, James Kuyper <> wrote:
    >> On 08/10/2011 12:51 PM, HumbleWorker wrote:
    >>
    >>> On Aug 10, 9:43 pm, tom st denis <> wrote:
    >>>> On Aug 6, 1:25 pm, HumbleWorker <> 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.
     
    James Kuyper, Aug 10, 2011
    #12
  13. HumbleWorker

    HumbleWorker Guest

    On Aug 10, 9:43 pm, tom st denis <> wrote:
    > On Aug 6, 1:25 pm, HumbleWorker <> 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;
    }
     
    HumbleWorker, Aug 10, 2011
    #13
  14. HumbleWorker

    tom st denis Guest

    On Aug 10, 1:29 pm, HumbleWorker <> wrote:
    > On Aug 10, 10:17 pm, (Ben Pfaff) wrote:
    >
    >
    >
    >
    >
    >
    >
    > > HumbleWorker <> writes:
    > > > On Aug 10, 9:57 pm, James Kuyper <> wrote:
    > > >> On 08/10/2011 12:51 PM, HumbleWorker wrote:

    >
    > > >> > On Aug 10, 9:43 pm, tom st denis <> wrote:
    > > >> >> On Aug 6, 1:25 pm, HumbleWorker <> 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
     
    tom st denis, Aug 10, 2011
    #14
  15. James Kuyper <> writes:
    > On 08/10/2011 12:51 PM, HumbleWorker wrote:
    >> On Aug 10, 9:43 pm, tom st denis <> wrote:
    >>> On Aug 6, 1:25 pm, HumbleWorker <> 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) <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"
     
    Keith Thompson, Aug 10, 2011
    #15
  16. HumbleWorker

    James Kuyper Guest

    On 08/10/2011 02:49 PM, Keith Thompson wrote:
    > James Kuyper <> 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.
     
    James Kuyper, Aug 10, 2011
    #16
  17. HumbleWorker

    James Kuyper Guest

    On 08/10/2011 02:26 PM, HumbleWorker wrote:
    > On Aug 10, 9:43 pm, tom st denis <> 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.
     
    James Kuyper, Aug 10, 2011
    #17
  18. James Kuyper <> writes:
    > On 08/10/2011 02:49 PM, Keith Thompson wrote:
    >> James Kuyper <> 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) <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"
     
    Keith Thompson, Aug 10, 2011
    #18
  19. HumbleWorker

    Seebs Guest

    On 2011-08-10, HumbleWorker <> 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 /
    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.
     
    Seebs, Aug 11, 2011
    #19
  20. tom st denis wrote:
    > On Aug 10, 1:29 pm, HumbleWorker <> wrote:
    >>>>>> On Aug 10, 9:43 pm, tom st denis <> 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
     
    J. J. Farrell, Aug 11, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HumbleWorker

    Any solution shorter than this ?

    HumbleWorker, Aug 6, 2011, in forum: C Programming
    Replies:
    4
    Views:
    314
    John Gordon
    Aug 6, 2011
  2. Lowell Kirsh
    Replies:
    5
    Views:
    112
    Lowell Kirsh
    Aug 18, 2005
  3. Dr.Ruud
    Replies:
    1
    Views:
    171
    Ted Zlatanov
    Feb 21, 2013
  4. George Mpouras
    Replies:
    0
    Views:
    161
    George Mpouras
    Feb 17, 2013
  5. C.DeRykus
    Replies:
    0
    Views:
    141
    C.DeRykus
    Feb 19, 2013
Loading...

Share This Page