Header Files (.h)

This is a discussion on Header Files (.h) within the C Programming forums, part of the General Programming Boards category; Hi I have question on header files. I know you can make your own header file i.e. # include “myfile ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    7

    Header Files (.h)

    Hi I have question on header files. I know you can make your own header file i.e. # include “myfile .h”. How do I use it in a program dose it have to be in the same folder as all the other .h . I checked out the FAQ and they did explain much. I just downloaded allegro and want to try some game programming.
    Mach

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >How do I use it in a program dose it have to be in the same folder as all the other .h
    User defined headers should be in the same path as the source files calling them, or in some other agreed upon location. What you do is put your declarations in a header, include the header in the source, write a separate implementation file if the header has functions, and compile the implementation file, linking it with the driver:
    Code:
    /* header.h */
    #ifndef FUNKSHUNHEADER
    #define FUNKSHUNHEADER
    
    void funkshun ( );
    
    #endif
    
    /* header.c */
    #include "header.h"
    
    void funkshun ( ) {
      /* Do something */
    }
    
    /* main.c */
    #include "header.h"
    
    int main ( ) {
      funkshun();
    
      return 0;
    }
    Code:
    $ gcc -o main main.c header.c
    And POOF! it all works.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>it all works.
    My turn for a nitpik: This:
    void funkshun ( );
    won't work for C89. You should be declaring functions with void if they don't require parameters:
    Code:
    void funkshun (void);
    
    void funkshun (void)
    {
      /* Stuff here */
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >My turn for a nitpik: This:
    Why is everyone always (nit)picking on me?

    >won't work for C89.
    Incorrect, it'll work just fine for C89. However, technically it eschews argument checking by saying the the number and type of arguments are unknown.

    >You should be declaring functions with void if they don't require parameters:
    Yea, I should. But knowing how things work and choosing my own style isn't quite the same as using a style out of ignorance, wouldn't you agree?

    p.s. When I write something big, I'll follow the rules more closely to save problems later, but my usual quick and dirty style doesn't do any damage, and it's faster than adhering completely to the standard on every line.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Why is everyone always (nit)picking on me?
    Why not?

    >>it'll work just fine for C89.
    OK, it will, but it ain't good practice. For those following the thread (if there is anyone!), here is why it's not good:
    Code:
    void foo( )
    {
    }
    int main(void)
    {
      foo();
      foo(11, 12, 13);
      return(0);
    }
    The compiler doesn't know how many arguments foo() takes, and therefore isn't required to complain about the two different uses of it in main(). Had it been declared void foo(void), the compiler would complain about the second call in main().

    Some compilers will issue a warning with the above code, for example, Borland 5.5 says:
    Code:
    Call to function 'foo' with no prototype in function main
    >>But knowing how things work and choosing my own style isn't quite the same as using a style out of ignorance, wouldn't you agree?
    Maybe, but.... The code is safer with void in the definition, I wouldn't call that a choice of style. And, "knowing how things work" is what most people come here to learn, so showing them the correct way is always a good thing.

    >>it's faster than adhering completely to the standard on every line.
    Faster by 4 key presses, eh!

    Anyway, enough picking on my part.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >OK, it will, but it ain't good practice.
    I'm here to get a job done, not make sure I'm practicing goodness.

    >foo();
    >foo(11, 12, 13);
    This really assumes that the user programmer is a moron. I mean, if you see empty parens in a function prototype, you've got to be pretty dense to try and pass it arguments.

    >The code is safer with void in the definition
    Naturally, safer is much better for projects larger than a few hundred lines. In those situations it's possible in theory to not be able to find the prototype, or the definition, and not bother to look it up because you "know" what parameters it takes.

    >I wouldn't call that a choice of style.
    Style is as style does.

    >Faster by 4 key presses, eh!
    Indeed! That's four punches at the keyboard that could be used for more productive things. Anyway, I meant faster in general, not just specific to not using void for function tags...but I digress. On to other matters!
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  2. Missing header files and libraries with Borland
    By quagsire in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 06:19 AM
  3. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  4. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 08:56 AM

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