Re: I fixed it!!!! here is the final code.

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 12/17/03


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



Relevant Pages

  • Re: c interview
    ... undefined behaviour because printf is a varidac function. ... stdio.h before using printf and other headers as appropriate before ... Would make it a point that the size of int is assumption to be .... ...
    (comp.lang.c)
  • Re: HPGCC installation issues on w98 w/ cygwin
    ... the headers. ... I thought about fiddling with the paths in the hpgcc.bat. ... from the cygwin installation into the hpgcc installation's bin ... hiworld.c:19: warning: implicit declaration of function 'printf' ...
    (comp.sys.hp48)
  • Re: Pointers in C
    ... int x, y; ... pointed out, so I won't belabour it here), the call to printf is not. ... provide a valid function prototype within the current scope at the ... Headers are not decorative. ...
    (comp.lang.c)
  • Re: definition of stdio.h
    ... i want to see the definition of printf and scanf and where it is ... The definition won't be in any header file, but the declaration ... But the standard headers are written for the compiler to use, ...
    (comp.lang.c)
  • Re: To: Richard Heathfield
    ... the way that unsigned and char are. ... meaning that no headers are needed ... in many other headers, like. ... I wondered about that because I used printf and sizeof with just stdio.h ...
    (comp.lang.c)