James [on his mailserver] wrote:

> why doesn't this work:
> 
> {
>       int c;
> 
>       for (c=0; c==9; c++)
>               /* something */
> }
> 
> i tried it once and it didn't work as expected (i can't remember what
> happened, it either looped for ever, or just skipped it).

It will never execute the body of the loop.

        for (c=0; c==9; c++)
                <something>

is (sort of) equivalent to

        c=0;
        while (c==9)
        {
                <something>
                c++;
        }

except that the former will always execute the `c++', even if
`continue' is executed.

> and, is this a Bad Thing...
> 
> {
>       for (int c=0; c<10; c++)
>               /* stuff */
> }

No. This is the usual way to write `FOR i = 0 TO 9' type loops.

> And here's something that my C Programming tutor does:
> 
> main ()
> {
> /* whatever */
> exit (0);
> }
> 
> shouldn't it really be
> 
> int main ()
> {
> /* things */
> exit (0);
> 
> return (0);
> }

It should be

        int main(void)
        {
                /* things */
                return 0;
        }

Using exit() from within main() is redundant. And even if you use
exit(), you should still have a `return' statement. The compiler can't
necessarily know that exit() will cause a non-local exit.

> to conform to the ANSI C standard? doing gcc -Wall picks this up (type of
> main defaults to int) and i know this is just not done:
> 
> void main (void)
> {
> /* things */
> exit (0);
> }

Allowing main() to return void is a gcc extension.

> someone recommend a good C reference book (reference, not tutorial...)

        The C Programming Language (2nd Edition)
        Brian W Kernighan & Dennis M Ritchie
        Prentice Hall

Note that this doesn't cover style. You can't really have a
`reference' book for style.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to