Thread: C and .h files -- how do they work // how do you use them?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    C and .h files -- how do they work // how do you use them?

    I'v had my guesses at how .h files work. I'v read up why to use them, however i'v not come across any good explanation that says "Here dummy, this is how you make them work".

    My initial guess is, put prototype in a text document, name it "function.h" and put #include "function.h" and it would just pull it from the text document (in the same folder, of course).

    Doesn't work, nor the other ways I tried. Can someone give me a brief explanation on how exactly you use the header files? I mean, i'd love to make my code look nicer, and i'm sure having LESS stuff on my page would help greatly.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    .h files are, generally, used to explain how something works, and .c files actually do the work.

    Your headers will potentially contain prototypes (so functions can be properly called), structs and typedefs (so types your functions make use of can be used), macros (such as “#define PATH_MAX 1024”); things of that nature.

    If you have a function that, say, gets a username, you might need to use that from various source files you have. So you have a file username.h:
    Code:
    #define MAX_USERNAME_LEN 16
    int get_user_name(char *);
    You'll also have a .c file (perhaps called username.c) that implements the get_user_name() function. It includes the .h file, because it might make use of MAX_USERNAME_LEN, and so you can type-check the function: if you accidentally implement the function as returning void, but the header says int, your compiler will complain. This is a good thing.

    Then, in each of your .c files that needs to call get_user_name(), you #include "username.h". username.h should be in the same directory as everything else, although you can usually tell your compiler to look in various different places for headers. Once a .c file includes username.h, it can create a buffer of the appropriate size (because MAX_USERNAME_LEN is visible), and it knows how to call the function (because get_user_name() is visible).

    All #include does is paste the contents of one file inside another. It'd be the same as copying the contents of a header in place of the #include, but of course using #include is much less error prone. Sometimes, in old code, you'll see .c files that manually include declarations for functions that are defined in other .c files. That should be avoided (for maintenance reasons), and header files are the way to avoid it.

    You will, in your .h file, want to use include guards: these prevent a header from being included multiple times in the same translation unit (for example, you could #include "foo.h" and #include "bar.h", but foo.h also does #include "bar.h", which would copy it in twice). That's as simple as:
    Code:
    #ifndef PROJECTPREFIX_HEADERNAME_H
    #define PROJECTPREFIX_HEADERNAME_H
    
    /* header contents go here */
    
    #endif
    You can use any legal macro name as an include guard. Do not start the name with an underscore, though: you'll see a lot of people do that, but it's wrong. Such a name belongs to the implementation, and you should not violate its namespace.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Does this mean header files go into a specific type of filer, or is it just a text document? Because from what i'v tried, it didn't work.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NinjaFish View Post
    Does this mean header files go into a specific type of filer, or is it just a text document? Because from what i'v tried, it didn't work.
    Mind if I ask you a bit of a question here?
    Do you actually own a C book or do you have links to tutorials?

    I mean no offense but you seem to be on here a lot asking questions that are more than adequately covered in most every C language learning tool I've ever seen... This isn't a class room and as you saw earlier today, some of us are truly lousy teachers...

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    it didn't work.
    This phrase is next to meaningless. There's really no way to troubleshoot something when absolutely no information is given about the problem.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by CommonTater View Post
    Mind if I ask you a bit of a question here?
    Do you actually own a C book or do you have links to tutorials?

    I mean no offense but you seem to be on here a lot asking questions that are more than adequately covered in most every C language learning tool I've ever seen... This isn't a class room and as you saw earlier today, some of us are truly lousy teachers...
    More or less, all my C primer Plus 5th edition book tells me about .h files is why you'd use them, how not to use them, and how to call them in a program. I'v not come across anything in it. And every tutorial I find online for .h files seems to do the exact same. Will tell you how they work, why you'd use them, but not how i'd store them or in what.

    But your right. I could go look at my book, and continually ask it questions until it decided to explain it to me. Maybe it's one of these new-fangled books, that isn't an inanimate object... wait.. yes it is, nevermind.

    If I can't find tutorials on what i'm trying to understand, I much rather prefer if someone can explain what I don't understand, rather than sit there for hours trying to brute force my way through a problem after 2 hours.


    Quote Originally Posted by cas View Post
    This phrase is next to meaningless. There's really no way to troubleshoot something when absolutely no information is given about the problem.
    And sorry, it's just telling me that it's not declared with the prototype.
    Last edited by NinjaFish; 04-18-2011 at 06:52 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NinjaFish View Post
    More or less, all my C primer Plus 5th edition book tells me about .h files is why you'd use them, how not to use them, and how to call them in a program. I'v not come across anything in it. And every tutorial I find online for .h files seems to do the exact same. Will tell you how they work, why you'd use them, but not how i'd store them or in what.

    But your right. I could go look at my book, and continually ask it questions until it decided to explain it to me. Maybe it's one of these new-fangled books, that isn't an inanimate object... wait.. yes it is, nevermind.

    If I can't find tutorials on what i'm trying to understand, I much rather prefer if someone can explain what I don't understand, rather than sit there for hours trying to brute force my way through a problem after 2 hours.
    Maybe it's just me... but it seems like some days half the threads here are yours and we seem to be answering a lot of questions that should be covered in any reasonable text book. But, that said... if you have legitimate questions... carry on.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by NinjaFish View Post
    My initial guess is, put prototype in a text document, name it "function.h" and put #include "function.h" and it would just pull it from the text document (in the same folder, of course).
    Yes that's pretty much it. However, some compiler environments expect all #include files to reside in a designated directory/folder. There may be a parameter that's settable to point to this folder. It could be somewhere different from where the .c is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. work with files in С99
    By sanda in forum C Programming
    Replies: 4
    Last Post: 05-06-2010, 03:17 AM
  2. trying to work with 2 files at same time
    By morngrym in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2010, 10:59 AM
  3. How do headers and other c files work
    By noob in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 07:04 PM
  4. learning to work on files
    By cmay in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 12:50 PM
  5. How do you work with resource files???
    By Finchie_88 in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2004, 02:39 PM