Multiple files

Discussion in 'C Programming' started by fb, Sep 2, 2003.

  1. fb

    fb Guest

    One quick question about multiple files.

    Let's say I had the following:

    #include <stdio.h>

    void Heading(void);

    int main(void)
    {
    printf("Hello. I'm about to call my Heading() function\n\n");
    Heading();
    printf("Now I'm back inside \"main\".\n");
    return 0;
    }

    void Heading(void)
    {
    printf("Hello! You are currently inside the Heading() ");
    printf("function.\n");
    }

    How would I split this up into seperate files? Would I need a header
    file (.h) and a seperate (.c) file in addition to the main (.c) file
    that has the main() function in it?

    What would go where?
    Thanks!

    fb goes looking for his missing C book...
    fb, Sep 2, 2003
    #1
    1. Advertising

  2. On Tue, 02 Sep 2003 09:53:19 GMT
    fb <> wrote:
    > What would go where?
    > Thanks!


    It could go all in the same directory:

    headingheader.h:
    void Heading(void);

    mainfile.c:
    #include <stdio.h>

    #include "headingheader.h"

    int main(void)
    {
    printf("Hello. I'm about to call my Heading() function\n\n");
    Heading();
    printf("Now I'm back inside \"main\".\n");
    return 0;
    }

    headingfile.c:
    #include <stdio.h>

    #include "headingheader.h"

    void Heading(void)
    {
    printf("Hello! You are currently inside the Heading() ");
    printf("function.\n");
    }

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
    Pieter Droogendijk, Sep 2, 2003
    #2
    1. Advertising

  3. fb <> wrote in <jMZ4b.77063$>:

    >One quick question about multiple files.
    >
    >Let's say I had the following:
    >
    >#include <stdio.h>
    >
    >void Heading(void);
    >
    >int main(void)
    >{
    > printf("Hello. I'm about to call my Heading() function\n\n");
    > Heading();
    > printf("Now I'm back inside \"main\".\n");
    > return 0;
    >}
    >
    >void Heading(void)
    >{
    > printf("Hello! You are currently inside the Heading() ");
    > printf("function.\n");
    >}
    >
    >How would I split this up into seperate files? Would I need a header
    >file (.h) and a seperate (.c) file in addition to the main (.c) file
    >that has the main() function in it?

    Exactly.

    >What would go where?

    You would like to put the prototype in a header file (e.g. 'heading.h'),
    which you #include in 'main.c', then put the function declaration in a
    separate .c file (e.g. 'heading.c'), then compile and link the two.

    >Thanks!
    >
    >fb goes looking for his missing C book...

    Good luck! :)

    Irrwahn
    --
    Air is water with holes in it.
    Irrwahn Grausewitz, Sep 2, 2003
    #3
  4. Irrwahn Grausewitz <> wrote in
    <>:
    >>What would go where?

    >You would like to put the prototype in a header file (e.g. 'heading.h'),
    >which you #include in 'main.c', then put the function declaration in a
    >separate .c file (e.g. 'heading.c'), then compile and link the two.
    >

    Reading P.D.'s reply I've noticed I forgot to mention that you should
    #include "heading.h" in 'heading.c' as well...

    [I have to be more careful with the 'Send' button anyway.]
    --
    Proofread carefully to see if you any words out.
    Irrwahn Grausewitz, Sep 2, 2003
    #4
  5. fb

    Kevin Bracey Guest

    In message <jMZ4b.77063$>
    fb <> wrote:

    > One quick question about multiple files.


    ---heading.h---

    void Heading(void);

    ---heading.c---

    #include <stdio.h>
    #include "heading.h"

    /* You should include your own header file to guarantee errors */
    /* if prototypes don't match. */

    void Heading(void)
    {
    printf("Hello! You are currently inside the Heading() ");
    printf("function.\n");
    }

    ---main.c---

    #include <stdio.h>
    #include "heading.h"

    int main(void)
    {
    printf("Hello. I'm about to call my Heading() function\n\n");
    Heading();
    printf("Now I'm back inside \"main\".\n");
    return 0;
    }

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
    Kevin Bracey, Sep 2, 2003
    #5
  6. fb <> writes:

    > One quick question about multiple files.
    >
    > Let's say I had the following:
    >
    > #include <stdio.h>
    >
    > void Heading(void);
    >
    > int main(void)
    > {
    > printf("Hello. I'm about to call my Heading() function\n\n");
    > Heading();
    > printf("Now I'm back inside \"main\".\n");
    > return 0;
    > }
    >
    > void Heading(void)
    > {
    > printf("Hello! You are currently inside the Heading() ");
    > printf("function.\n");
    > }
    >
    > How would I split this up into seperate files? Would I need a header file
    > (.h) and a seperate (.c) file in addition to the main (.c) file that has the
    > main() function in it?
    >
    > What would go where?


    Only declarations (including function prototypes) should go into header
    files. If you want to define your `Heading' function in a separate file,
    write a corresponding header file with the prototype, and include it both
    where the function is defined and used.

    Martin


    ---- heading.h ----

    #ifndef H_HEADING
    #define H_HEADING

    void Heading(void);

    #endif


    ---- heading.c ----

    #include <stdio.h>
    #include "heading.h"

    void Heading(void)
    {
    printf("Hello! You are currently inside the Heading() ");
    printf("function.\n");
    }


    ---- myprog.c ----

    #include <stdio.h>
    #include "heading.h"

    int main(void)
    {
    printf("Hello. I'm about to call my Heading() function\n\n");
    Heading();
    printf("Now I'm back inside \"main\".\n");
    return 0;
    }
    Martin Dickopp, Sep 2, 2003
    #6
  7. Irrwahn Grausewitz <> writes:

    > then put the function declaration in a separate .c file

    ^^^^^^^^^^^
    ITYM "definition". :)

    Martin
    Martin Dickopp, Sep 2, 2003
    #7
  8. Martin Dickopp <> wrote in
    <bj1v4t$fc$06$-online.com>:

    >Irrwahn Grausewitz <> writes:
    >
    >> then put the function declaration in a separate .c file

    > ^^^^^^^^^^^
    >ITYM "definition". :)

    Oh dear, you're absolutely right!

    --
    Air is water with holes in it.
    Irrwahn Grausewitz, Sep 2, 2003
    #8
  9. fb

    fb Guest

    Martin Dickopp wrote:
    > fb <> writes:
    >
    >
    >>One quick question about multiple files.
    >>
    >>Let's say I had the following:
    >>
    >>#include <stdio.h>
    >>
    >>void Heading(void);
    >>
    >>int main(void)
    >>{
    >> printf("Hello. I'm about to call my Heading() function\n\n");
    >> Heading();
    >> printf("Now I'm back inside \"main\".\n");
    >> return 0;
    >>}
    >>
    >>void Heading(void)
    >>{
    >> printf("Hello! You are currently inside the Heading() ");
    >> printf("function.\n");
    >>}
    >>
    >>How would I split this up into seperate files? Would I need a header file
    >>(.h) and a seperate (.c) file in addition to the main (.c) file that has the
    >>main() function in it?
    >>
    >>What would go where?

    >
    >
    > Only declarations (including function prototypes) should go into header
    > files. If you want to define your `Heading' function in a separate file,
    > write a corresponding header file with the prototype, and include it both
    > where the function is defined and used.
    >
    > Martin
    >
    >
    > ---- heading.h ----
    >
    > #ifndef H_HEADING
    > #define H_HEADING
    >
    > void Heading(void);
    >
    > #endif
    >
    >
    > ---- heading.c ----
    >
    > #include <stdio.h>
    > #include "heading.h"
    >
    > void Heading(void)
    > {
    > printf("Hello! You are currently inside the Heading() ");
    > printf("function.\n");
    > }
    >
    >
    > ---- myprog.c ----
    >
    > #include <stdio.h>
    > #include "heading.h"
    >
    > int main(void)
    > {
    > printf("Hello. I'm about to call my Heading() function\n\n");
    > Heading();
    > printf("Now I'm back inside \"main\".\n");
    > return 0;
    > }


    Ok. Would it be possible to put all my prototypes and definitions into
    one header file? I suppose that would be bad form though...right?
    fb, Sep 2, 2003
    #9
  10. fb <> wrote in <n195b.78696$>:
    <SNIP>
    >... Would it be possible to put all my prototypes and definitions into
    >one header file? I suppose that would be bad form though...right?


    It's of course possible, and for a small projects probably the best
    way - you don't want to end up with one header file per prototype or
    so, do you? For larger projects there is a "rule" to have one header
    file per module, and maybe one header file to gather all the #include
    directives you need for your main (or even another) module. This way
    it's even more easy to reuse a module in other projects, without
    having to find out which parts of your header file you really need -
    it's already well sorted.
    However, for small projects, or if you are a beginner, it's better to
    have all your prototypes and declarations in one place - this way you
    won't get confused about where to look for, say, a specific function
    prototype and you can concentrate on your task.
    OTOH, if your single header file grows to big, you should divide it
    in logical units and put these in separate files - even in small
    projects you may end up with sth. like 'proto.h' and 'def.h', or the
    like.
    Anyway, it's up to you to modularize [is this english or germish?]
    your project to your own preferences and needs. It is definetly worth
    thinking about /before/ you actually start writing code.

    After all, there is no one-for-all rule for this.

    Irrwahn
    --
    Rain is just liquid sunshine.
    Irrwahn Grausewitz, Sep 3, 2003
    #10
  11. fb

    Jack Klein Guest

    On Tue, 2 Sep 2003 12:51:09 +0200, Pieter Droogendijk
    <> wrote in comp.lang.c:

    > On Tue, 02 Sep 2003 09:53:19 GMT
    > fb <> wrote:
    > > What would go where?
    > > Thanks!

    >
    > It could go all in the same directory:


    C doesn't have directories, although many platforms that host C
    compilers do. And I fail to see what directories have to do with it,
    anyway. I could create the files you describe below on two different
    computers on two different continents, and still make this work with a
    proper path argument to the compiler.

    > headingheader.h:
    > void Heading(void);
    >
    > mainfile.c:
    > #include <stdio.h>
    >
    > #include "headingheader.h"
    >
    > int main(void)
    > {
    > printf("Hello. I'm about to call my Heading() function\n\n");
    > Heading();
    > printf("Now I'm back inside \"main\".\n");
    > return 0;
    > }
    >
    > headingfile.c:
    > #include <stdio.h>
    >
    > #include "headingheader.h"
    >
    > void Heading(void)
    > {
    > printf("Hello! You are currently inside the Heading() ");
    > printf("function.\n");
    > }


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Sep 3, 2003
    #11
  12. fb

    Kevin Easton Guest

    fb <> wrote:
    [...]
    > Ok. Would it be possible to put all my prototypes and definitions into
    > one header file? I suppose that would be bad form though...right?


    You shouldn't put definitions of external-linkage objects or functions
    into header filse at all - the reason is that if the header file is
    included in more than one .c file in the project, as is usual, then you
    will have multiple external definitions which is a no-no. In general,
    definitions go in .c files, and declarations go in .h files.

    - Kevin.
    Kevin Easton, Sep 3, 2003
    #12
  13. fb <> writes:

    > Ok. Would it be possible to put all my prototypes and definitions

    ^^^^^^^^^^^
    It is certainly a bad idea to put /definitions/ in a header file. Only
    /declarations/ belong there.

    > into one header file? I suppose that would be bad form though...right?


    The best solution, in my opinion, is to have a corresponding header file
    for every source file. The header file declares the public interface of
    the functionality implemented in the source file, i.e. functions that are
    supposed to be called from other source files, `struct's if needed, etc.

    If you're delevoping a library, that might be a possible exception. The
    users of your library would probably find it inconvenient to have to
    include a separate header file for every single function, so it might
    make sense to group similar functions together. This is what the standard
    C library does: it doesn't have <printf.h>, <fprintf.h>, <fopen.h>, etc.,
    but it has <stdio.h>, which declares a number of functions dealing with
    a certain "topic".

    Martin
    Martin Dickopp, Sep 3, 2003
    #13
  14. fb

    LibraryUser Guest

    fb wrote:
    >

    .... snip ...
    >
    > Would it be possible to put all my prototypes and definitions into
    > one header file? I suppose that would be bad form though...right?


    You should think of header files as defining the **published**
    interface to the code and data specified in a corresponding .c
    file. At the same time you can take the precaution of declaring
    other entities in the .c file as static (functions and file scope
    variables) to ensure they cannot be externally accessed. This
    will simplify future maintenance.

    You can have multiple .h files for a specific .c file, thus
    allowing finer control of external visibility. However this does
    not prevent name clashes.

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation.
    LibraryUser, Sep 3, 2003
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crazyprakash
    Replies:
    4
    Views:
    3,360
    adrian
    Oct 30, 2005
  2. JHenstay
    Replies:
    3
    Views:
    539
    osmium
    Jan 11, 2004
  3. Replies:
    4
    Views:
    932
    M.E.Farmer
    Feb 13, 2005
  4. karthikbalaguru
    Replies:
    3
    Views:
    2,995
    Chris Dollin
    Nov 27, 2008
  5. thufir
    Replies:
    3
    Views:
    202
    Thufir
    Apr 12, 2008
Loading...

Share This Page