Question about (Optional)? header files.

Discussion in 'C Programming' started by mdh, Dec 4, 2007.

  1. mdh

    mdh Guest

    K&R briefly mention splitting programs into seperate files.(page 82)
    Using my compiler (X-code) I noticed that creating a header file
    gives, as expected one .h file, but creating a new ".c" file creates
    an additional file, same name, but with an ".h" extension, and calls
    this an optional header file.
    Compiling the program using both the .c and .h ( with appropriate
    #includes) executes successfully. In K&R, .h files were used to
    centralize declarations, etc, and each .c file was used to define
    functions etc.
    Could anyone explain what is happening here, and perhaps suggest good
    usage? ( I looked at the FAQ, and unless I missed it, I did not see
    this addressed.)
    As usual, thanks in advance.
     
    mdh, Dec 4, 2007
    #1
    1. Advertising

  2. mdh

    santosh Guest

    mdh wrote:

    > K&R briefly mention splitting programs into seperate files.(page 82)
    > Using my compiler (X-code) I noticed that creating a header file
    > gives, as expected one .h file, but creating a new ".c" file creates
    > an additional file, same name, but with an ".h" extension, and calls
    > this an optional header file.
    > Compiling the program using both the .c and .h ( with appropriate
    > #includes) executes successfully. In K&R, .h files were used to
    > centralize declarations, etc, and each .c file was used to define
    > functions etc.
    > Could anyone explain what is happening here, and perhaps suggest good
    > usage? ( I looked at the FAQ, and unless I missed it, I did not see
    > this addressed.)
    > As usual, thanks in advance.


    Generally header files are used for exporting and importing declarations
    and definitions. Say that you have implemented a regular expression
    parser library in files r0.c, r1.c and r2.c. Obviously these modules
    will need to share some common declarations. A data structure might
    need to be global, appropriate typedefs may need to be present and r0.c
    may call functions from r1.c, r1.c from r2.c and in other combinations.
    Here is where a header file comes into use. You place all the
    appropriate declarations into a header, say r.h, and #include this into
    all the necessary .c files.

    Also your library will export an interface to the world which will need
    to be put into one or more headers and supplied along with your source
    files.

    So, in summary, headers either export some functionality to the outside
    world, or import functionality into modules from other modules or serve
    as a place to put declarations and definitions that will be used by
    multiple modules, like macros and feature test conditionals etc.

    The Standard library headers are themselves a good illustration of the
    concepts involved. Unfortunately the heavy use of compiler magic causes
    most headers from mainstream C libraries to be rather difficult for a
    beginner to understand.
     
    santosh, Dec 4, 2007
    #2
    1. Advertising

  3. mdh <> writes:
    > K&R briefly mention splitting programs into seperate files.(page 82)
    > Using my compiler (X-code) I noticed that creating a header file
    > gives, as expected one .h file, but creating a new ".c" file creates
    > an additional file, same name, but with an ".h" extension, and calls
    > this an optional header file.
    > Compiling the program using both the .c and .h ( with appropriate
    > #includes) executes successfully. In K&R, .h files were used to
    > centralize declarations, etc, and each .c file was used to define
    > functions etc.
    > Could anyone explain what is happening here, and perhaps suggest good
    > usage? ( I looked at the FAQ, and unless I missed it, I did not see
    > this addressed.)


    This wouldn't be mentioned in the FAQ, because it's not a C issue.

    I'm not familiar with X-code, but I suspect it's some kind of IDE, not
    just a compiler. Apparently it chooses to generate a .h file for you
    in some circumstances. Such a file should be no different from one
    you could have written manually.

    The compiler itself doesn't care how any soure file was generated.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 4, 2007
    #3
  4. mdh

    mdh Guest

    On Dec 4, 1:46 pm, Keith Thompson <> wrote:
    >
    > The compiler itself doesn't care how any soure file was generated.
    >




    Thank you Keith and Santosh.
     
    mdh, Dec 4, 2007
    #4
  5. mdh

    CBFalconer Guest

    mdh wrote:
    >

    .... snip ...
    >
    > Compiling the program using both the .c and .h ( with appropriate
    > #includes) executes successfully. In K&R, .h files were used to
    > centralize declarations, etc, and each .c file was used to define
    > functions etc.


    You are misusing header files. Their purpose is to expose elements
    of a .c file that are needed in other .c files. They are, for
    example, the method of promulgating the prototype of a function to
    other .c files. Things that are not needed externally should not
    be mentioned in the .h file, and in fact should often be marked
    static in the .c file.

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


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 5, 2007
    #5
  6. >>>>> "KT" == Keith Thompson <> writes:

    KT> I'm not familiar with X-code, but I suspect it's some kind of
    KT> IDE, not just a compiler.

    Xcode (spelling counts!) is Apple's IDE, which uses a customized
    version of gcc as its compiler. (I *think* the customizations all
    relate to Objective-C, but I wouldn't swear to it.) There's a lot of
    DWIM in it.

    The best place to ask about Xcode proper is the xcode-users list at Apple.

    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, Dec 5, 2007
    #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. Piers Chivers
    Replies:
    2
    Views:
    407
    Piers Chivers
    Mar 2, 2004
  2. MENTAT
    Replies:
    8
    Views:
    983
    MENTAT
    Apr 4, 2005
  3. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    624
    Jack Klein
    Jul 24, 2004
  4. Replies:
    3
    Views:
    1,329
    Walter Roberson
    May 1, 2006
  5. mlt
    Replies:
    2
    Views:
    880
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page