Re: Stylistic note on loops

Discussion in 'C Programming' started by Stefan Ram, Oct 23, 2010.

  1. Stefan Ram

    Stefan Ram Guest

    Eric Sosman <> writes:
    >On 10/21/2010 7:40 PM, Stefan Ram wrote:
    >>In C, to skip two space-separated integral numerals
    >>(under certain circumstances):
    >>while( isnum( *++c )); while( isspace( *++c )); while( isnum( *++c ));

    >(...) Personally, I cannot recall a single occasion in more than forty
    >years of programming when I (1) wanted to skip over two numbers this
    >way and (2) wanted to ignore what those numbers were and (3) had such


    I have been looking for similar loops »in the wild«:

    A sequence of skipping scanning while loops:

    while (!isspace(*p)) ++p; while (isspace(*p)) ++p;

    http://github.com/mono/mono/blob/master/libgc/os_dep.c

    While loops with a semicolon (and no corresponding »do« in front of them):

    while (*c && *c++!='\n');

    http://www.opensource.apple.com/source/man/man-4/man/man2html/man2html.c

    Well, and I believe:

    while( *p++ = *q++ );

    is an idiom known to every C programmer.
     
    Stefan Ram, Oct 23, 2010
    #1
    1. Advertising

  2. "christian.bau" <> wrote in message
    news:...
    On Oct 23, 1:36 am, -berlin.de (Stefan Ram) wrote:

    > Well, and I believe:
    >
    > while( *p++ = *q++ );
    >
    > is an idiom known to every C programmer.


    <--
    Many compilers will give you a warning for the ";" immediately
    following the closing parenthesis without at least white space in
    between - likelyhood that the semicolon is there by mistake is too
    high. For example

    while( *p++ = *q++);
    count++;

    is surely a bug.
    -->

    not that I have seen...


    in most C and C++ code this practice is sufficiently common that it would
    make a lot of code start spewing warnings all over the place if compilers
    were to warn about it.

    but, then again, some compilers will warn about unused local variables,
    which is personally a bit annoying.
    hence, it becomes fairly common to ignore many warning conditions until they
    become errors if the compiler is intent on giving too many warnings over
    pointless crap and one doesn't feel like spending all the extra time needed
    to go clean them up...

    it is better when warnings are confined to more serious issues, like using
    an uninitialized variable, ...


    warning: implicit conversion from 'void *' to 'Foo *' without a cast; ...
    hence the dreaded "casting the result of malloc" event (some people get
    really fussy for or against casting the results of malloc calls...).


    admittedly, using a while-loop to perform a copy or similar this way is
    generally ugly (usually strcpy or memcpy is preferable, and often faster).

    much like, 'strcmp()' is the usual way of copying strings, and is generally
    preferable to hand-rolled loops.

    but, there are many other cases where one will end up using loops like this.
     
    BGB / cr88192, Oct 23, 2010
    #2
    1. Advertising

  3. Stefan Ram

    BartC Guest

    "Keith Thompson" <> wrote in message
    news:...

    > "BGB / cr88192" <> writes:
    > [...]
    >> but, then again, some compilers will warn about unused local variables,
    >> which is personally a bit annoying.


    > You find warnings about unused local variable annoying? Why?
    > Is there any reason to declare a variable if you're not going to
    > use it?


    Where code is incomplete, or in development, declarations can exist but not
    the statements that use them (or they might be commented out temporarily).

    For example, if I've just written a complicated set of declarations, I might
    compile to check for errors, before I've written any actual code.

    This sort of warning is useful when development has finished.

    --
    Bartc
     
    BartC, Oct 23, 2010
    #3
  4. "BartC" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> "BGB / cr88192" <> writes:
    >> [...]
    >>> but, then again, some compilers will warn about unused local variables,
    >>> which is personally a bit annoying.

    >
    >> You find warnings about unused local variable annoying? Why?
    >> Is there any reason to declare a variable if you're not going to
    >> use it?

    >
    > Where code is incomplete, or in development, declarations can exist but not
    > the statements that use them (or they might be commented out temporarily).
    >
    > For example, if I've just written a complicated set of declarations, I might
    > compile to check for errors, before I've written any actual code.
    >
    > This sort of warning is useful when development has finished.


    Or during maintenance, which typically continues long after development
    has "finished".

    Personally, I want to be warned about unused variables unless I
    ask not to be. Most compilers let you disable specific warnings.

    --
    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, Oct 23, 2010
    #4
  5. Stefan Ram

    Ian Collins Guest

    On 10/24/10 11:20 AM, BartC wrote:
    >
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >> "BGB / cr88192" <> writes:
    >> [...]
    >>> but, then again, some compilers will warn about unused local variables,
    >>> which is personally a bit annoying.

    >
    >> You find warnings about unused local variable annoying? Why?
    >> Is there any reason to declare a variable if you're not going to
    >> use it?

    >
    > Where code is incomplete, or in development, declarations can exist but
    > not the statements that use them (or they might be commented out
    > temporarily).
    >
    > For example, if I've just written a complicated set of declarations, I
    > might compile to check for errors, before I've written any actual code.


    If you are doing that, your functions are too long or complex. If the
    code as written does not require a variable, don't declare it.

    --
    Ian Collins
     
    Ian Collins, Oct 24, 2010
    #5
  6. "BGB / cr88192" <> might have writ, in
    news:i9v341$2so$:

    > ... some compilers will warn about unused local
    > variables, which is personally a bit annoying.


    I once used a "language" where some unused variables, believe it or not,
    caused Fatal Errors!! I even complained about it on Usenet in
    Message <> :

    http://groups.google.com/group/comp.unix.wizards/msg/3774732a57ef112d?
    hl=en&dmode=source

    (People e-mailed me in response to that post, saying they'd printed
    it and taped it to the wall!)

    > "christian.bau" <> wrote:
    >> while( *p++ = *q++ );
    >>
    >> is an idiom known to every C programmer.


    I prefer
    while (*p++ = *q++) {
    }

    James Dow Allen
     
    James Dow Allen, Oct 31, 2010
    #6
  7. On Oct 23, 6:39 pm, "BGB / cr88192" <> wrote:
    > "christian.bau" <> wrote in message
    >
    > but, there are many other cases where one will end up using loops like this.
    >

    for(i=0;i<N;i++)
    /* do work here */

    or

    for(i=0;array != sentinel;i++)
    /* do work here */

    is my preferred method, because it doesn't corrupt the pointers. In
    the olden days indexing used to be a little bit slower because the
    compiler would create an extra variable and offset calculation.
    However that's no longer much of a concern.
     
    Malcolm McLean, Oct 31, 2010
    #7
  8. On 23 Oct, 23:12, Ian Collins <> wrote:
    > On 10/24/10 11:20 AM, BartC wrote:
    >
    >
    >
    >
    >
    >
    >
    > > "Keith Thompson" <> wrote in message
    > >news:...

    >
    > >> "BGB / cr88192" <> writes:
    > >> [...]
    > >>> but, then again, some compilers will warn about unused local variables,
    > >>> which is personally a bit annoying.

    >
    > >> You find warnings about unused local variable annoying? Why?
    > >> Is there any reason to declare a variable if you're not going to
    > >> use it?

    >
    > > Where code is incomplete, or in development, declarations can exist but
    > > not the statements that use them (or they might be commented out
    > > temporarily).

    >
    > > For example, if I've just written a complicated set of declarations, I
    > > might compile to check for errors, before I've written any actual code.

    >
    > If you are doing that, your functions are too long or complex.  If the
    > code as written does not require a variable, don't declare it.


    sometimes you're forced to declare paramters you don't use (eg.
    callback functions)
     
    Nick Keighley, Oct 31, 2010
    #8
  9. On 31.10.2010 13:32, Richard wrote:
    > James Dow Allen <> writes:
    >>> "christian.bau" <> wrote:
    >>>> while( *p++ = *q++ );
    >>>>
    >>>> is an idiom known to every C programmer.

    >>
    >> I prefer
    >> while (*p++ = *q++) {
    >> }
    >>
    >> James Dow Allen

    >
    > Why? That's ridiculous.
    >
    > The point of having no {} is that you realise that all the *serious*
    > stuff is going on inside the while loop statement itself.


    I prefer

    while (*p++ = *q++)
    ;

    The empty statement where the single statement always goes. Can not be
    seen as a typo.

    --

    Don't relax! It's only your tension that's holding you together.
     
    Donkey Hottie, Oct 31, 2010
    #9
  10. Stefan Ram

    Ian Collins Guest

    On 11/ 1/10 02:54 AM, Nick Keighley wrote:
    > On 23 Oct, 23:12, Ian Collins<> wrote:
    >> On 10/24/10 11:20 AM, BartC wrote:
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>> "Keith Thompson"<> wrote in message
    >>> news:...

    >>
    >>>> "BGB / cr88192"<> writes:
    >>>> [...]
    >>>>> but, then again, some compilers will warn about unused local variables,
    >>>>> which is personally a bit annoying.

    >>
    >>>> You find warnings about unused local variable annoying? Why?
    >>>> Is there any reason to declare a variable if you're not going to
    >>>> use it?

    >>
    >>> Where code is incomplete, or in development, declarations can exist but
    >>> not the statements that use them (or they might be commented out
    >>> temporarily).

    >>
    >>> For example, if I've just written a complicated set of declarations, I
    >>> might compile to check for errors, before I've written any actual code.

    >>
    >> If you are doing that, your functions are too long or complex. If the
    >> code as written does not require a variable, don't declare it.

    >
    > sometimes you're forced to declare paramters you don't use (eg.
    > callback functions)


    The topic was unused local variables, not unused parameters.

    --
    Ian Collins
     
    Ian Collins, Oct 31, 2010
    #10
  11. Stefan Ram

    Ian Collins Guest

    On 11/ 1/10 12:32 AM, Richard wrote:
    > James Dow Allen<> writes:
    >
    >> "BGB / cr88192"<> might have writ, in
    >> news:i9v341$2so$:
    >>
    >>> ... some compilers will warn about unused local
    >>> variables, which is personally a bit annoying.

    >>
    >> I once used a "language" where some unused variables, believe it or not,
    >> caused Fatal Errors!! I even complained about it on Usenet in
    >> Message<> :
    >>
    >> http://groups.google.com/group/comp.unix.wizards/msg/3774732a57ef112d?
    >> hl=en&dmode=source
    >>
    >> (People e-mailed me in response to that post, saying they'd printed
    >> it and taped it to the wall!)
    >>
    >>> "christian.bau"<> wrote:
    >>>> while( *p++ = *q++ );
    >>>>
    >>>> is an idiom known to every C programmer.

    >>
    >> I prefer
    >> while (*p++ = *q++) {
    >> }
    >>
    >> James Dow Allen

    >
    > Why? That's ridiculous.
    >
    > The point of having no {} is that you realise that all the *serious*
    > stuff is going on inside the while loop statement itself.


    It's a great way to confuse readers!

    --
    Ian Collins
     
    Ian Collins, Oct 31, 2010
    #11
  12. Nick Keighley <> writes:
    > On 23 Oct, 23:12, Ian Collins <> wrote:

    [...]
    >> If you are doing that, your functions are too long or complex.  If the
    >> code as written does not require a variable, don't declare it.

    >
    > sometimes you're forced to declare paramters you don't use (eg.
    > callback functions)


    There's usually a way to silence warnings about unused parameters, such
    as

    (void)unused_param;

    (But there's no *standard* way to do so.)

    --
    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, Oct 31, 2010
    #12
  13. Stefan Ram

    Eric Sosman Guest

    On 10/31/2010 3:04 PM, Keith Thompson wrote:
    >
    > There's usually a way to silence warnings about unused parameters, such
    > as
    >
    > (void)unused_param;
    >
    > (But there's no *standard* way to do so.)


    There is no *standard* way to silence any warning whatsoever,
    including "scary.c: trick or treat!"

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 31, 2010
    #13
  14. Stefan Ram

    Stefan Ram Guest

    Ian Collins <> writes:
    >>The point of having no {} is that you realise that all the *serious*
    >>stuff is going on inside the while loop statement itself.

    >It's a great way to confuse readers!


    Indeed, most people will be confused by this, since most
    people do not know the C programming language at all.
    However, a C programmer will not be confused by this (by
    definition).

    Have you ever tried to speak for someone from whom you know
    that he can not understand English very well? You will have
    noticed that you language becomes crippled and unnatural.

    We must write code that is as readable as possible. But
    we may assume that the reader

    - knows C and

    - reads every single character of the source code,

    otherwise writing readable code is impossible (because we
    cannot know upfront which specific part of C a specific
    reader will not know or which specific part of our source
    code he will miss to read).

    One exercise of my programming class for C beginners is
    to predict the output of this program:

    #include <stdio.h>

    int main()
    { printf( "1\n" ); /* writes "1" *
    printf( "2\n" ); * writes "2" */ }

    .
     
    Stefan Ram, Oct 31, 2010
    #14
  15. Stefan Ram

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >{ printf( "1\n" ); /* writes "1" *
    > printf( "2\n" ); * writes "2" */ }


    Or, what do you think about the following variant?
    (I am not sure if it's still valid and portable C.)

    { printf( "1\n" ); /* writes "1" *\
    printf( "2\n" ); \* writes "2" */ }
     
    Stefan Ram, Oct 31, 2010
    #15
  16. Stefan Ram

    Eric Sosman Guest

    On 10/31/2010 3:22 PM, Stefan Ram wrote:
    > -berlin.de (Stefan Ram) writes:
    >> { printf( "1\n" ); /* writes "1" *
    >> printf( "2\n" ); * writes "2" */ }

    >
    > Or, what do you think about the following variant?
    > (I am not sure if it's still valid and portable C.)
    >
    > { printf( "1\n" ); /* writes "1" *\
    > printf( "2\n" ); \* writes "2" */ }


    I'm not entirely convinced of the pedagogical value, but
    if you think this sort of thing is useful consider:

    printf ("2\n"); /*
    printf ("3\n"); * Print all the
    printf ("5\n"); * 1-digit primes
    printf ("7\n"); */

    Throw in a red herring, if you wish:

    printf ("2\n"); /*
    printf ("3\n"); * Print all the
    printf ("5\n"); * 1-digit primes
    printf ("9\n"); */

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 31, 2010
    #16
  17. Stefan Ram

    Ian Collins Guest

    On 11/ 1/10 08:17 AM, Stefan Ram wrote:
    > Ian Collins<> writes:
    >>> The point of having no {} is that you realise that all the *serious*
    >>> stuff is going on inside the while loop statement itself.

    >> It's a great way to confuse readers!

    >
    > Indeed, most people will be confused by this, since most
    > people do not know the C programming language at all.
    > However, a C programmer will not be confused by this (by
    > definition).


    Restoring the context:

    James Dow Allen <> writes:

    >
    > I prefer
    > while (*p++ = *q++) {
    > }
    >


    The confusion stems not from the syntax, but from the empty braces, did
    he intend there to be some code in there?

    --
    Ian Collins
     
    Ian Collins, Oct 31, 2010
    #17
  18. Stefan Ram

    Ian Collins Guest

    On 11/ 1/10 09:48 AM, superpollo wrote:
    > Ian Collins ha scritto:
    >> On 11/ 1/10 08:17 AM, Stefan Ram wrote:
    >>> Ian Collins<> writes:
    >>>>> The point of having no {} is that you realise that all the *serious*
    >>>>> stuff is going on inside the while loop statement itself.
    >>>> It's a great way to confuse readers!
    >>>
    >>> Indeed, most people will be confused by this, since most
    >>> people do not know the C programming language at all.
    >>> However, a C programmer will not be confused by this (by
    >>> definition).

    >>
    >> Restoring the context:
    >>
    >> James Dow Allen <> writes:
    >>
    >> >
    >> > I prefer
    >> > while (*p++ = *q++) {
    >> > }
    >> >

    >>
    >> The confusion stems not from the syntax, but from the empty braces,
    >> did he intend there to be some code in there?

    >
    > and what if sometime in the future you need to meat up a bit the body of
    > the loop?


    You add some. I don't see the relevance of the question.

    --
    Ian Collins
     
    Ian Collins, Oct 31, 2010
    #18
  19. -berlin.de (Stefan Ram) writes:
    > -berlin.de (Stefan Ram) writes:
    >>{ printf( "1\n" ); /* writes "1" *
    >> printf( "2\n" ); * writes "2" */ }

    >
    > Or, what do you think about the following variant?
    > (I am not sure if it's still valid and portable C.)
    >
    > { printf( "1\n" ); /* writes "1" *\
    > printf( "2\n" ); \* writes "2" */ }


    Yes, it's valid. The backslash on the end of the first line is
    a line-splicing character if it's not followed by white space, or
    just part of the comment if it is, but the program's meaning is the
    same either way. (I had thought it might affect later occurrences
    of __LINE__, but the line number is defined in terms of physical
    lines, not logical lines.)

    --
    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, Oct 31, 2010
    #19
  20. Ian Collins <> writes:
    > On 11/ 1/10 08:17 AM, Stefan Ram wrote:
    >> Ian Collins<> writes:
    >>>> The point of having no {} is that you realise that all the *serious*
    >>>> stuff is going on inside the while loop statement itself.
    >>> It's a great way to confuse readers!

    >>
    >> Indeed, most people will be confused by this, since most
    >> people do not know the C programming language at all.
    >> However, a C programmer will not be confused by this (by
    >> definition).

    >
    > Restoring the context:
    >
    > James Dow Allen <> writes:
    >
    > >
    > > I prefer
    > > while (*p++ = *q++) {
    > > }
    > >

    >
    > The confusion stems not from the syntax, but from the empty braces, did
    > he intend there to be some code in there?


    One possible alternative is:

    while (*p++ = *q++) {
    /* nothing */
    }

    Another is:

    while (*p++ = *q++) {
    continue;
    }

    --
    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, Oct 31, 2010
    #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. P.Hill
    Replies:
    13
    Views:
    570
    Adam Maass
    Apr 23, 2004
  2. Denis Remezov
    Replies:
    4
    Views:
    488
    Siemel Naran
    Apr 30, 2004
  3. Mantorok Redgormor

    stylistic prototype question

    Mantorok Redgormor, Oct 29, 2003, in forum: C Programming
    Replies:
    5
    Views:
    369
    Mark A. Odell
    Oct 30, 2003
  4. Andrew Koenig

    Stylistic question about inheritance

    Andrew Koenig, Mar 31, 2005, in forum: Python
    Replies:
    17
    Views:
    502
    Guy Bolton King
    Apr 1, 2005
  5. markspace

    Stylistic note on loops

    markspace, Oct 21, 2010, in forum: Java
    Replies:
    221
    Views:
    3,492
Loading...

Share This Page