Thread: Making header files in c?

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    Making header files in c?

    I have a problem with making header files in c, i get the code written only in main.c and then i have to create a files with extension .h and extension .c but i have no idea how to do it.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Do you mean how to change the file extensions or what to put inside each? For the first if using windows, you have to uncheck "hide extensions of know file types" in the "folder and search options". Then you'll be able to change extensions manually.

    About what to put inside them, it's quite simple. In .h you put the prototypes( declarations ) and in .c you put the implementation( definitions ). Then you need to compile them together,
    • either by adding all files to a project ( main.c, example.h & example.c ) and building,
    • in cmd by giving all source files to the compiler (gcc main.c example.c ...)
    • or by building object files(*.o) for each source with (gcc -c main.c -o main.o) and linking them all together (gcc main.o example.o -o example.exe).
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    690
    Quote Originally Posted by cdummie View Post
    I have a problem with making header files in c, i get the code written only in main.c and then i have to create a files with extension .h and extension .c but i have no idea how to do it.
    I assume you mean how to create a header file and include it into your source file. If so, please see the following tutorial:

    C - Header Files tutorial

  4. #4
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    Well, what confuses me is that when i create a header file i should have that ifndef part automatically created, yet, when i do it, it's just empty file, what should i do to make it appear right after i create header file?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That depends on what IDE you are using and if the IDE in question is capable of working with template files.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making changes to a header
    By gL_nEwB in forum C++ Programming
    Replies: 7
    Last Post: 05-14-2006, 04:48 PM
  2. Making header files
    By Sentral in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 07:56 PM
  3. Help with making header files
    By AdamLAN in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2005, 12:23 AM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  5. Making Classes header files.
    By RealityFusion in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2003, 11:54 AM