Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: program bug
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 36 Topic 26044 of 26977
Post > Topic >>

Re: program bug

by Keith Thompson <kst-u@[EMAIL PROTECTED] > May 3, 2008 at 02:48 PM

"Bill Cunningham" <nospam@[EMAIL PROTECTED]
> writes:
> "Robert Gamble" <rgamble99@[EMAIL PROTECTED]
> wrote in message 
> news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@[EMAIL PROTECTED]
>
> [snip]
>
>
>> I think you want braces around these statements, yes?
>
>     I don't know. I've ran code before and seen a lot of people use
while 
> and statements after it and the code works. No braces. So I usually use
it 
> like that. It's not a typo I don't usually use braces with while.
[...]

Do you understand what the braces mean?

Here's the tail end of the code Robert was commenting on:

[...]
 while(a!=EOF)
 a=fgetc(ifp);
 fputc(a,ofp);
 printf("done\n");
 return 0;}

Putting the closing brace immediately after the semicolon like that is
horribly bad style.  Putting everything at the same indentation level
is also horribly bad style.  The compiler doesn't care, but anyone
trying to read your code does.

The syntax of a while statement is:

    while ( expression ) statement

Note that that's a *single* statement.  If you want the body to be
multiple statements, you need to wrap them in braces to create a
compound statement, which is itself as single statement.  (The same
thing applies to if statements; you correctly used braces for those,
so I don't know why the while loop was such a problem.)

The code I quoted is equivalent to this:

[...]
    while (a != EOF)
        a = fgetc(ifp);
    fputc(a, ofp);
    printf("done\n");
    return 0;
}

With proper indentation, you can see that the while controls only a
single statement, ``a = fgetc(ifp);''.  The fputc call will occur
exactly once, after the while loop terminates.  I can't be certain,
but I *think* you wanted both the fgetc and fputc calls to be in the
body of the loop.  To do that, you need braces:

[...]
    while (a != EOF) {
        a = fgetc(ifp);
        fputc(a, ofp);
    }
    printf("done\n");
    return 0;
}

In my opinion, it's a good habit always to use braces even if you only
want only a single statement in the body.  It makes the code more
consistent and easier to maintain if you later want to add a second
statement.  (Others will disagree.)

Judicious use of whitespace, especially around operators and after
commas, also makes the code much easier to read.

From now on, if you post a chunk of nearly illegible code like what
you posted upthread, with no indentation and virtually no whitespace,
I will ask you to format it properly before I'll even consider helping
you fix any other problems.

-- 
Keith Thompson (The_Other_Keith) <kst-u@[EMAIL PROTECTED]
>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
 




 36 Posts in Topic:
program bug
"Bill Cunningham&quo  2008-05-02 21:40:19 
Re: program bug
"Bill Cunningham&quo  2008-05-02 21:53:34 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:08:03 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:05:58 
Re: program bug
"Bill Cunningham&quo  2008-05-02 22:16:27 
Re: program bug
CBFalconer <cbfalconer  2008-05-02 18:47:28 
Re: program bug
"Bill Cunningham&quo  2008-05-02 22:21:04 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:28:04 
Re: program bug
"Bill Cunningham&quo  2008-05-03 20:42:30 
Re: program bug
Keith Thompson <kst-u@  2008-05-03 14:48:48 
Re: program bug
"Bill Cunningham&quo  2008-05-03 22:58:24 
Re: program bug
Keith Thompson <kst-u@  2008-05-03 18:08:44 
Re: program bug
"Default User"   2008-05-04 03:20:33 
Re: program bug
Keith Thompson <kst-u@  2008-05-03 21:17:13 
Re: program bug
"Default User"   2008-05-04 19:03:07 
Re: program bug
vippstar@[EMAIL PROTECTED  2008-05-03 16:21:48 
Re: program bug
CBFalconer <cbfalconer  2008-05-03 20:29:01 
Re: program bug
Joe Wright <joewwright  2008-05-02 18:25:33 
Re: program bug
Ian Collins <ian-news@  2008-05-03 10:28:17 
Re: program bug
"Bill Cunningham&quo  2008-05-02 22:36:04 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:36:31 
Re: program bug
Ian Collins <ian-news@  2008-05-03 11:31:53 
Re: program bug
Joe Wright <joewwright  2008-05-02 20:18:17 
Re: program bug
Ian Collins <ian-news@  2008-05-03 12:31:09 
Re: program bug
"Bill Cunningham&quo  2008-05-02 23:15:17 
Re: program bug
Keith Thompson <kst-u@  2008-05-02 23:56:46 
Re: program bug
Richard Heathfield <rj  2008-05-03 07:04:24 
Re: program bug
CBFalconer <cbfalconer  2008-05-02 18:50:15 
Re: program bug
user923005 <dcorbit@[E  2008-05-02 21:25:53 
Re: program bug
"Bill Cunningham&quo  2008-05-03 15:52:27 
Re: program bug
vippstar@[EMAIL PROTECTED  2008-05-03 09:01:52 
Re: program bug
Eligiusz Narutowicz<el  2008-05-04 08:41:56 
Re: program bug
"Bill Cunningham&quo  2008-05-04 21:25:27 
Re: program bug
Barry Schwarz <schwarz  2008-05-04 15:20:34 
Re: program bug
"Bill Cunningham&quo  2008-05-06 02:30:36 
Re: program bug
Nick Keighley <nick_ke  2008-05-06 02:28:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:48:22 CDT 2008.