.h files

Discussion in 'C Programming' started by johnnash, Feb 21, 2008.

  1. johnnash

    johnnash Guest

    Ok I have a doubt regarding .h files. I basically have two modules in
    my software program -

    a.c and b.c

    There is .h file called d.h. d.h contains prototypes of functions in
    a.c so whenever i have to use functions of a.c i simply need to
    include d.h. My question is can i also add the prototypes of functions
    in b.c in d.h so that i only need to d.h in main.c so as to access
    functions of both a.c and b.c.
    johnnash, Feb 21, 2008
    #1
    1. Advertising

  2. On Wed, 20 Feb 2008 21:01:56 -0800 (PST), johnnash
    <> wrote:

    >Ok I have a doubt regarding .h files. I basically have two modules in
    >my software program -
    >
    >a.c and b.c
    >
    >There is .h file called d.h. d.h contains prototypes of functions in
    >a.c so whenever i have to use functions of a.c i simply need to
    >include d.h. My question is can i also add the prototypes of functions
    >in b.c in d.h so that i only need to d.h in main.c so as to access
    >functions of both a.c and b.c.


    Yes. Doing so will also provide the additional benefit that the
    compiler can verify the function prototype and function definition
    match.


    Remove del for email
    Barry Schwarz, Feb 21, 2008
    #2
    1. Advertising

  3. "johnnash" <> wrote in message
    news:...
    > Ok I have a doubt regarding .h files. I basically have two modules in
    > my software program -
    >
    > a.c and b.c
    >
    > There is .h file called d.h. d.h contains prototypes of functions in
    > a.c so whenever i have to use functions of a.c i simply need to
    > include d.h. My question is can i also add the prototypes of functions
    > in b.c in d.h so that i only need to d.h in main.c so as to access
    > functions of both a.c and b.c.
    >

    You can, but it is best to put prototypes in a .h file named after the .c
    file in which they are defined.

    So you have a.c, a.h, b.c, b.h, and maybe AAA.h containing structures common
    to both a.c and b.c.

    Now define all.h

    #include "AAA.h"
    #include "a.h"
    #include "b.h"

    if you just want to include just one header in your main.c file.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 21, 2008
    #3
  4. johnnash

    CBFalconer Guest

    johnnash wrote:
    >
    > Ok I have a doubt regarding .h files. I basically have two modules
    > in my software program - a.c and b.c
    >
    > There is .h file called d.h. d.h contains prototypes of functions
    > in a.c so whenever i have to use functions of a.c i simply need to
    > include d.h. My question is can i also add the prototypes of
    > functions in b.c in d.h so that i only need to d.h in main.c so as
    > to access functions of both a.c and b.c.


    No. You should match header files with source files, so each
    expresses the external access permitted to the content of that
    source file. You should have a.h an b.h, and #include the h files
    for the units you use.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 21, 2008
    #4
  5. CBFalconer wrote:
    >
    > johnnash wrote:
    > >
    > > Ok I have a doubt regarding .h files. I basically have two modules
    > > in my software program - a.c and b.c
    > >
    > > There is .h file called d.h. d.h contains prototypes of functions
    > > in a.c so whenever i have to use functions of a.c i simply need to
    > > include d.h. My question is can i also add the prototypes of
    > > functions in b.c in d.h so that i only need to d.h in main.c so as
    > > to access functions of both a.c and b.c.

    >
    > No. You should match header files with source files, so each
    > expresses the external access permitted to the content of that
    > source file. You should have a.h an b.h, and #include the h files
    > for the units you use.


    I disagree here, to a point. If a.c and b.c are related, perhaps as
    part of a library, then a single .h file should be used. Consider,
    for example, the stdlib.h header file, which contains prototypes of
    many functions. I would like to think (and I happen to know for a
    fact, on the systems I've checked) that these functions are actually
    defined in separate source modules.

    Now, if the only relationship a.c and b.c have is that they happen
    to both be part of this program that's being written, then it makes
    sense to have separate a.h and b.h files, and perhaps a wrapper file
    for the project that #include's both.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Feb 22, 2008
    #5
  6. johnnash

    CBFalconer Guest

    Kenneth Brody wrote:
    > CBFalconer wrote:
    >> johnnash wrote:
    >>
    >>> Ok I have a doubt regarding .h files. I basically have two modules
    >>> in my software program - a.c and b.c
    >>>
    >>> There is .h file called d.h. d.h contains prototypes of functions
    >>> in a.c so whenever i have to use functions of a.c i simply need to
    >>> include d.h. My question is can i also add the prototypes of
    >>> functions in b.c in d.h so that i only need to d.h in main.c so as
    >>> to access functions of both a.c and b.c.

    >>
    >> No. You should match header files with source files, so each
    >> expresses the external access permitted to the content of that
    >> source file. You should have a.h an b.h, and #include the h files
    >> for the units you use.

    >
    > I disagree here, to a point. If a.c and b.c are related, perhaps as
    > part of a library, then a single .h file should be used. Consider,
    > for example, the stdlib.h header file, which contains prototypes of
    > many functions. I would like to think (and I happen to know for a
    > fact, on the systems I've checked) that these functions are actually
    > defined in separate source modules.
    >
    > Now, if the only relationship a.c and b.c have is that they happen
    > to both be part of this program that's being written, then it makes
    > sense to have separate a.h and b.h files, and perhaps a wrapper file
    > for the project that #include's both.


    If you want this I suggest you get it by: Create a.h and b.h. Now
    create all.h, which has the lines:

    #include "a.h"
    #include "b.h"

    possibly accompanied by various guards. Now you have full future
    flexibility.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 23, 2008
    #6
    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. Tom Hansen
    Replies:
    2
    Views:
    2,400
    Tom Hansen
    Nov 21, 2003
  2. Chad

    .vb files to .resx files

    Chad, Dec 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    307
  3. crazyprakash
    Replies:
    4
    Views:
    3,371
    adrian
    Oct 30, 2005
  4. Replies:
    4
    Views:
    952
    M.E.Farmer
    Feb 13, 2005
  5. Replies:
    3
    Views:
    1,461
    Rolf Magnus
    Jan 18, 2009
Loading...

Share This Page