Always use braces

Frans van Otten fransvo at htsa.uucp
Thu Jan 5 21:15:54 AEST 1989


David T. Sandberg ({amdahl,hpda}!bungia!nis!mictl!dts) mailed me about
my posting (edited):

<< #include <stdio.h>
<< 
<< int f()
<<   { int ch;
<<     while ((ch = getchar()) != EOF  &&  c)    /*  find EOF or '\0'  */
<<     return (ch);
<<   }
<
<Methinks thou needeth another semicolon, or thy while loop doth return
<after thy first iteration... also, what the undefined 'c' variable is
<intended to contain I won't guess, but in any event it seems useless
<to use a variable to store a constant expression.  Lastly, the
<conditional expression is parsed as ((ch = getchar()) != (EOF && c)),
<which finally compares the gotten character to the result of the
<Boolean expression (EOF && c)... either a 0 or a 1, depending on whether
<c is zero or nonzero.

I agree on the semicolon; the c should, of course, have been ch (How did
you guess that it was a variable ? :-) ). So far for the typos.  What you
write about the expression parsing is not true: In my C book it sais that
the != operator has a higher priority than the && operator, so the
expression will be parsed like:

  ( ( ( (ch = getchar() )  != EOF )  &&  ( ch ) )

Which makes it clear (to me) that the while loop terminates either at
EOF or when ch == '\0'.

<I understand the basis of what you are espousing, and agree that putting
<the whole test into the conditional expression is the way to go.  Here is
<what I would do... you'll note that it is closer to your idea than Doug's,
<but should work:
<
<#include <stdio.h>
<
<int func()
<}
<    int ch;
<    do {
<        ch = getchar();
<    } while ((ch != EOF) && (ch != '\0'));
<    return(ch);
<}

It won't work. Try an opening { to start the function block.
By the way, I never use a  do {} while ();  because I personally
don't like such a structure. But then again, I have more stupid
and unbased ideas.

<I'd post this as an article, but I don't have the capability to post
<news from this site.  ;'(  Feel free to quote from it and tear me limb
<from limb... I've got nothing better to do than read about it...  ;')

-- 
                         Frans van Otten
                         Algemene Hogeschool Amsterdam
			 Technische en Maritieme Faculteit
                         fransvo at htsa.uucp



More information about the Comp.lang.c mailing list