Re: I fixed it!!!! here is the final code.
From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 12/17/03
- Next message: Ben Pfaff: "Re: Undefined behaviour question"
- Previous message: Old Wolf: "Re: Newbie question about "malloc" ???"
- In reply to: Kevin Goodsell: "Re: I fixed it!!!! here is the final code."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 16 Dec 2003 23:18:14 -0500 (EST)
On Wed, 17 Dec 2003, Kevin Goodsell wrote:
>
> interpim wrote:
> > IN MAIN()
> >
> > #include "primes.h"
> >
> > int main()
>
> It's better to include 'void' inside the parens. '()' and '(void)' are
> very different. In the case of main() it's arguable whether it matters,
> but in general you should not declare functions with empty parameter
> lists unless you *really* know what you are doing.
<OT> Note that the exact opposite is common practice in C++, where
() and (void) *are* synonyms in this context. But C isn't C++.
Just thought I'd point that out in case there was any confusion. </OT>
<snip>
> if (is_prime(n))
> > {
> > printf("%5d: ",count+1);
> > printf("%5d\n",n);
> > ++count;
>
> You could move this above the printf(), and lose the '+1'. You could
> even combine all three of these statements this way:
>
> printf("%5d: %5d\n", ++count, n);
>
> But if you are more comfortable with this, I would understand:
>
> ++count;
> printf("%5d: %5d\n", count, n);
IMHO the latter is always better. I don't generally expect
my 'printf' calls to have side effects. Also, the former way
might bite you if you ever want to, say, print a newline after
every few numbers:
++count;
printf("%5d: %5d%s", count, n, ((count%10)? " ": "\n"));
versus
/* WRONG WRONG WRONG WRONG WRONG */
printf("%5d: %5d%s", ++count, n, ((count%10)? " ": "\n"));
> > }
> > }
> > }
> >
> >
> >
> > IN PRIMES.H
> >
> > #include <stdio.h>
> > #include <stdlib.h>
>
> I still think it's a bad idea to #include headers where they aren't
> needed, particularly in a header file (because it means you are forcing
> these to be #included in any code that uses your header, even if they
> aren't wanted or needed).
Right. Lose the headers. And add the missing include guards,
like so:
#ifndef H_PRIMES
#define H_PRIMES
...
#endif
>
> <snip>
>
HTH,
-Arthur
- Next message: Ben Pfaff: "Re: Undefined behaviour question"
- Previous message: Old Wolf: "Re: Newbie question about "malloc" ???"
- In reply to: Kevin Goodsell: "Re: I fixed it!!!! here is the final code."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|