Difference between .h and .c files

This is a discussion on Difference between .h and .c files within the C Programming forums, part of the General Programming Boards category; What's the difference between a .c file and a .h file? I though .h was just for modules, but I ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    Difference between .h and .c files

    What's the difference between a .c file and a .h file? I though .h was just for modules, but I hear people calling it header files. Also, everyone seems to have .h .c files for each module.

    What's the deal?

    Thanks!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    .h files are called header files, they should not contain any code (unless it happens to contain information about a C++ templated object). They typically contain function prototypes, typedefs, #define statements that are used by the source files that include them. .c files are the source files. They typically contain the source code implementation of the functions that were prototyped in the appropriate header file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    So, is it good practise to prototype your functions in header files? I've gotten away with it enough.

    a.h:

    void function();


    a.c
    void function(int *parameter)
    {

    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > void function();
    This isn't a prototype, it's an old-style function declaration.

    This is a prototype (for the example you list)
    void function(int *);

    Or even better
    void function(int *parameter);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    Quote Originally Posted by Salem
    void function(int *);
    Is that still a valid declaration? That's AT&T style, isn't it? Very scary, if you've seen a lot of 80s code.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    I've always kept my function bodies in .h files and included them as modules in my main.c file. I understand this is bad practise. Do I declare the function prototype in the header file, and then use a .c file with the same name as the header, and include the header in main.c to include my functions?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > Do I declare the function prototype in the header file, and then use a .c file with the same name as the header
    Yes, that's exactly it.

    Then you compile both source files together and the linker works its magic to combine both object files into a runnable program.

    > Is that still a valid declaration?
    As far as I know.
    Though I find omitting the variable name odd since it's extra work over simply copy/pasting the function definition, and you lose useful documentation in the process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Salem
    Though I find omitting the variable name odd since it's extra work over simply copy/pasting the function definition, and you lose useful documentation in the process.
    I always use the header declarations to find the functions I need to use that are in the c file. So taking away the variable names from a function definition is bad for me :(

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SMurf
    Quote Originally Posted by Salem
    void function(int *);
    Is that still a valid declaration? That's AT&T style, isn't it? Very scary, if you've seen a lot of 80s code.
    The only requirement of a function prototype is that it have a return value, function name, and an optional parameter type list. Variable names are optional.
    Quote Originally Posted by Kleid-0
    I always use the header declarations to find the functions I need to use that are in the c file. So taking away the variable names from a function definition is bad for me
    In fact, variable names need not even be the same. The following is legal:
    Code:
    #include <stdio.h>
    
    void foo( int bar );
    
    int main( void )
    {
        foo( 4 );
    
        return 0;
    }
    
    void foo( int baz )
    {
        printf("baz is %d\n", baz );
    }
    This will compile without warnings using "-Wall -ansi -pedantic".

    Variable names are purely optional, and as long as the types and order are the same in the actual definition, they can be called anything you want, provided it's a valid potential variable name.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What I often do is what quzah described above. I'll use a verbose parameter name in the header file so that, combined with comments, it becomes clear what the parameter is for. In the actual source file I'll use a normal meaningful variable name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .h file vs .c file
    By RocketMan in forum C Programming
    Replies: 5
    Last Post: 05-13-2008, 07:28 PM
  2. Replies: 4
    Last Post: 10-08-2007, 11:47 AM
  3. Replies: 4
    Last Post: 12-14-2005, 01:21 PM
  4. globals & multiple .c files
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-30-2002, 08:43 PM
  5. Multiple .c files
    By GaPe in forum Windows Programming
    Replies: 3
    Last Post: 03-26-2002, 07:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21