How to create header files in C/C++ ?

First of all, does creating a header file useful at all ? If yes how ?

It obviously is or else we wouldn’t have had soemthing called a ‘header files’.

Next, can we create our own header files ?

Yes, you can. It is pretty simple and its going to make your program simple and customizable.

Let us start with creating a header file in the traditional TurboC. Define the contents of the header file (meaning the functions you would like to include in the file) and then save it as a ‘.h’ file in the ‘include’ directory.

The path could be C:/TC/INCLUDE if you have installed Turbo C directly in ‘drive C’.

Your are done !! 🙂 Include the header file in the program by just including the file like any other file.

#include “headerfilename.h” or #include<headerfilename.h>

Creating one in GCC is a bit more difficult. Define the file in the same way as stated above and save it in a ‘directory where you are going to save the program’ (NOTE: This is important. Both the header file and the program must be in the same directory, if not the program will not be able to detect your header file ). Header File successfully created ! 🙂 But, unlike Turbo C the header file cannot be included by

#include<headerfilename.h>

The only way to include the header file is to treat the filename in the same way u treat a string.

#include “headerfilename.h”

Now that you have created your header files, you can create the function like sort, factorial etc. and store them in the header file. When you intend to use them, include the header file in the program and just call the function you stored in the header file.

Hasn’t it made life simple ? 😀

Advertisements

60 Comments »

  1. 1
    biswas Says:

    “Creating one in GCC is a bit more difficult” .. What is the difficulty?

    • 2
      crazycool5 Says:

      Difficult in the sense that one must ensure that the header file(created) and the program in which he has included the header file must adhere to the same folder.

      • 3
        Mike Says:

        Thanks for the tip. Was creating a header file for gcc compilation and didn’t know the file needed to be treated as a file. There is a way to specify the path to these newly created header files. During compilation there is a -I flag (capitol i) example:

        gcc -lstdc++ -I”PATH_TO_MY_HEADER_FILES” -o myProgram.exe main.cpp

        where in this case the quotations are needed and the path can be relative or absolute.

        I think you can also specify the path in the include statement for the program as well, but that might be asking for trouble.

        Hope this helps someone out here.

  2. 4
    suraj pachkduave Says:

    sir,

    i want to creat my own header file and i want to how creat function in header file . suggest me any book by creating header file .

    also i request you to suggest topic to creat c and c++ project

    your studnet
    suraj pachkudave

    • 5

      Dude, I am student as well…. No need to call me Sir… 😀

      You can create functions the same way you do it in your program. Just store it in a header file and when you want to use that funtion in your porgram, just include that header file (like you include stdio.h to use printf() ). I am not sure if you will find any books for creating header files.

      If you want information about project, follow up this site http://www.codeproject.com/

  3. 7
    Indranil Bose Says:

    As far as my knowledge is concerned, HEADER FILES contains only declaration of functions.How to implement this in GCC for self-made header files?Where does the body of the functions will reside?

    • 8
      kuldeep Says:

      no a header file contains both the defination and declRtion of function

      • 9
        Indranil Bose Says:

        Sorry Sir! For your kind information, CONVENTIONALLY a header file only contains declaration and not defination of functions. But you can keep defination too if you wish to. It will neither show an error nor a warning.

  4. 10
    Indranil Bose Says:

    How to create Library file for C functions through GCC?

  5. 11

    @ Indranil Bose : No, a header file contains both the function definition and declaration. You can verify it yourself by reading any of the standard C++ header file.

    The following links are useful in library files :

    http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
    http://www.network-theory.co.uk/docs/gccintro/gccintro_79.html

    • 12
      Mr.India Says:

      @ Praveen Kumar : I believe Indranil is right. A header file contains only the function declaration. The function definitions are present in object files or libraries which are linked during compile time.

      • 13

        How do you think Object files are created ??
        It is by compiling these header files. Next, if there are libraries containing definitions for a particular function then what is the need for a header file ? 😀

      • 14
        kunal Says:

        Hi Praveen, Header files should only contain the declaration and not the definition.
        Header files (.h) are designed to provide the information that will be needed in multiple files. Things like class declarations, function prototypes, and enumerations typically go in header files. In a word, “definitions”.

        Code files (.cpp) are designed to provide the implementation information that only needs to be known in one file. In general, function bodies, and internal variables that should/will never be accessed by other modules, are what belong in .cpp files. In a word, “implementations”.

        As for need for a separate header file, the main reason would be for separating the interface from the implementation. The header declares “what” a class (or whatever is being implemented) will do, while the cpp file defines “how” it will perform those features.
        This reduces dependencies so that code that uses the header doesn’t necessarily need to know all the details of the implementation and any other classes/headers needed only for that. This will reduce compilation times, and also the amount of recompilation needed when something in the implementation changes.
        Again this is what is followed in industry and your personal choices may depend on your coding style.

      • 15
        Pramodkumar Says:

        hey.
        praveen i need a help to create own library file and header files in C.

        i dont have any idea how to do this.
        can u help me……………………………..!

    • 16
      naidu Says:

      Hello I am naidu. what are the uses unionregs …..?

  6. 17
    Mr.India Says:

    Object files are created by compiling the respective source files. The header file contains the declarations alone. My understanding is based on the link which you specified.

    http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

    You can always write your implementation in your “own” header file. But, in general, header files provide only the interfaces/prototypes to use library functions. Anyway, show me an example of any predefined header file that contains any function definition. (Not macros)

  7. 18
    Yogendra Singh Shekhawat Says:

    @ Praveen,

    Praveen I am afraid of your confidence. Though you can define function in a header file but this is not their purpose, header files are used only for function prototype and external variable declaration. It is used to make a interface among object modules or files.
    Plz dude, it is nice you are teaching juniors but make sure you r not creating misconceptions for new students. It is a responsible task.

  8. 21
    Sanket Says:

    I want to make a header file such that uses cin function of c+ as only int. . That means if we includ dat header file and use cin functn then it shud use only int else error. . I tried bt it makes it var type so plz hlp me

    • 22

      I suggest that you can make that function with scanf. scanf can read only the data type specified in the argument but cin essentially removes the restriction. This was the sole purpose that lead to the creation of cin. I am not telling that what you want to do is not possible to it cin but it is unnecessary complication. 🙂 I hope you understood !

  9. 23
    PURUSHOTHAMAN Says:

    Hi,
    Help me to Providing (or) Creating the header files such stdio,conio for my turbo c/c++…

  10. 24
    Haleem Says:

    Guys can anyone help me in defining classes in header files, I mean what information we have to give in header file?

  11. 25
    JUNAID Says:

    VERY NICE METHOD FOR CREATING HEADER FILE..THNX A LOT

  12. 26
    patty Says:

    the header files which are already present in the lib folder are having obj extension. so if i create a header file and placed it in the lib folder, i have to give to some path to my porgram. plz tell me how to assign path to my program so that i can make thausands of header files, and one day my own c…

  13. 27
    suhas Says:

    you are a fool!

  14. 28
    NEERAJ SETHI Says:

    HIIIIII ANYONE HELP ME PLZ I WANT TO MAKE A HEADERFILE IN C SO MAIL ME PLZZZZ ,
    MADDYSETHI@GMAIL.COM

  15. 29
    ranjith Says:

    sir,
    long int m=400*400/400;
    The above statement (in C) is giving something diff result.Can u plz xplain the reason………….

    mekalaranjithkumar@gmail.com,
    M.Ranjith,
    M.C.A,Hyd.

    • 30
      kunal Says:

      @Ranjith
      I can compile the code correctly on my GCC compiler.
      #include
      int main()
      {
      long int m=400*400/400;
      printf(“%ld”,m);
      return 0;
      }

      Prints 400 for me.
      What compiler are you using?

  16. 31

    sir ,
    m a big fan of urs ….
    i hv seeen ur ol da vdeos n ur written files ,….
    dey ol r awesum ….
    i jz luv da way of ur teaching …..
    keep it up sir ….

  17. 32
    gentlin Says:

    sir,

    How to include mysql header files in turbo C to make connection?

    Thanks,

  18. 33
    chetna sharma Says:

    hi. I want to create my header file of differential and integral functions. want to create through switch cases. can you please tell me the proper procedure of creating it or suggest me any book by creating header file

  19. 34
    Sahoo Says:

    Thank You Sir, this made my life easy..

  20. 35
    Amit Says:

    Hi,

    I am getting error “Unable to include header file”
    Path for header files I know but where should I provide it so c++ can pick the files.

    please help.

  21. 36
    Amit Says:

    Done I got it….

    it is in Options -> Directories

  22. 37
    osssman Says:

    Write bites in Turbo c++ Header (“Include”) Files

  23. 38
    Sumanta Banerjee Says:

    But How can I create a header file of my own? eg. suppose I’ve created a function starfield(). and want it to define it to a new header file say USER.H. how is this possible. You are saying about how to include an extenal header file[not present in c:\tc\include] . and named this topic “Creating header File”.. which hardly makes any sense.. :-I

    • 39

      very simple……just define ur function starfiled() and save it under name of USER.h when ur just writing normal program just #include”USER.h” and call ur function.

  24. 40

    Thank you…! It really worked…

  25. 41
    sarita Says:

    i want ask you how i add my header file in turbo c/c++ which is on window?

    • 42
      Indranil Says:

      @Sarita : What you can do is … keep the ” *.h ” (header file) … in a specific directory … (it is preferred that you keep it in the same directory where you have kept the ” *.C ” file – just PREFERRED and NOT a rule), and in you C file include the header file by giving proper path of the header file in ” #include “. You may also use Relative paths (‘ . ‘ for current folder and ‘ .. ‘ for parent folder)

  26. 43
    Shelly Says:

    jb ata ni h to btate kyu ho suar ki aulad

  27. 44
    Manoj Says:

    Praveen Please suggest me the steps to create header files and how can I define my function in the header files please reply me as soon as possible with example of your own making header files

  28. 45
    rekha kumawat Says:

    how can we make our headfile in c

  29. 46
    supriya Says:

    plz suggest steps to create a header file……

  30. 47
    varadharajan Says:

    Hi.
    I have three 3 files. func.h and func.c contains declaration and definition resp. and i tried to use that function through user.c by #include “func.h”. but the linker didn’t link the function.
    Please try this in turbo c/c++ in windows and give a reply.

  31. 48
    mahendra Says:

    if we only give function declaration in header files then where do we declare those functions n how do we link everything to our program…??

    • 49

      No, you should give the complete function definition in the header file !

    • 50
      Indranil Says:

      Declaration of functions in .h files ( i.e., header files ) and definitions in in any lib folder you create for you program, you have to mention it’s path, that’s it … it is generally (*.c for C) and (*.cpp for C++)

  32. 51
    Dandy Says:

    Готовые домашние задания.

  33. 52
    Taufique Says:

    First of all write the definition of function in notepad/notepad++ and save it in include as File_Name.h extension in include (go TC—>include).now use this function using include in another program…….

  34. 53
    bharat Says:

    hi….!
    Can u give me the simpliest way to scroll the console screen in C and C++.
    if u can plz give me as soon as u can.

  35. 54
    hardik vyas Says:

    thanks for giving me this info.

  36. 55
    sumeet Says:

    for you kind information…..header files have only function declaration (also structs, enums etc)….also if you look at the definition of header file…it says that it only includes declaration i.e prototypes…not the actual definition of them…….don’t create a wrong notion in the course of simplifying the things….check out the rules for a header file….

  37. 56
    Raman Stha Says:

    Thanks dude

  38. 57
    Siddharth Burman Says:

    I am not able to create my header file in spite of following the above instructions

  39. 58
    Arunpradhap Says:

    Dear praveen can C file having main ( ) function be the the header file of another

    C file having main( ) function

  40. 59
    KISHORKUMAR Says:

    I liked the description…

  41. 60
    kishan Says:

    I am getting linker error in below program
    Sample. H contains add (int, int)
    Sample. Cpp contains definition of add
    Use.cpp calls add
    Send me solution vaishnavkishan16@gmail.com


RSS Feed for this entry

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: