Re: Ahead of "main"?



Barry Schwarz wrote, On 30/04/07 12:00:
On Sun, 29 Apr 2007 23:48:23 +0100, Flash Gordon
<spam@xxxxxxxxxxxxxxxxxx> wrote:

Stephen Sprunk wrote, On 29/04/07 22:35:

<snip>

As a rule, you should declare all functions before you call them; I won't go into the reasons why, as Eric did a good job of that. One style is to define all other functions before main(), which also declares them. The other is to declare all of your functions in a group near the beginning of the source, and then you can define them in any order you want. The latter style is effectively required when you move to multi-file projects, and the standard practice is to put all of your function declarations in header (.h) files so that each source file can simply #include the appropriate header files and then use whatever functions are needed.
Actually, you should not put *all* your function definitions in header files, since generally there are some which should be local to a given source file and declared static in that source file. I.e. you should always limit visibility to the smallest unit that makes sense, since then you don't have to look as far to see all of the usage.

You should not put any function definitions in a header file. I would
go a step further and say no object definitions either. Only
declarations. The only definitions in a header file should be typedef
(oops, even though it is called a type definition in the standard, it
is specifically described as a declaration) and macro definitions.

It was a trypo. I meant you should not put all your function declarations in your headers.
--
Flash Gordon
.



Relevant Pages

  • Re: Function prefix comments in C files
    ... > source file equals one module, or is some variation on this theme. ... > file, using "extern" declarations. ... You typically have a couple of stand-alone header files ... Where globals are regrettably present, it is reasonable to gather them ...
    (comp.arch.embedded)
  • Re: Linker error multiple definitions
    ... > I declared the functions in separate header files ... But your compiler isn't complaining about multiple *declarations*. ...
    (comp.lang.c)
  • Re: Linker error - fooView.obj LNK2005: _IID_IProvideTaskPage already defined in fooDoc.obj
    ... declarations, one in the document and one in the view. ... multithreaded libraries would refer to symbols in the single-threaded and multithreaded ... Unless someone in the header files is ... Look for simple solutions first. ...
    (microsoft.public.vc.mfc)
  • Re: [patch 1/3] epoll cleanups - epoll include diet ...
    ... and it certainly needs to have those syscall's prototypes in scope. ... Ack about signal.h, I forgot about the pwait code:( ... So that the compiler can verify that our declarations of sys_epoll_foo ... include in .c the header files which provide the stuff ...
    (Linux-Kernel)
  • Re: passing structures among functions
    ... I used 'wget' to grab the GIF, so if there's any malicious code on that Web page, I didn't encounter it. ... Give the struct a tag, such as "struct data", and use that instead. ... "Header files should only contain declarations." ...
    (comp.lang.c)