Create own C header file

Posted on 2004-11-04
Last Modified: 2008-02-07
   I have written several string manipulation functions in C. I don't want to have to keep this in my main program module for re-usability. I know that these can be stored in a seperate .c file and a header file created in order to access these functions. What I don't know is exactly how to go about it?

e.g. I have the following four functions

void replace(char *pch_string, char ch_replace_char, char ch_with_char);

void ucase(char *pch_string);

void lcase(char *pch_string);

void trim(char *pch_string);

Can anyone give me a step by step guide as to what I need to put in the header/.c file/calling module to make this work. Also where should the finished .h and .c files be stored. I am developing my code using VC++ 6.0 although once written it will be ported to a UNIX platform.

Many Thanks,

Question by:dustybryn
    LVL 45

    Accepted Solution

    Just put them in a file with a ".h" extension.  You might also include a few "standard" wrappers so that in can be included several times.

    /*   MyFile.h  */

    #if !define(MYFILE_H)
    #define MYFILE_H

    /*  Include your definitions here  */


    Then within your program, simply do:

    #include "MyFile.h"    /*  or  */
    #incluee <MyFile.h>  /*  Depending on where you put the file  */

    Good Luck,

    Author Comment

    Cheers for this. Can you explain what exactlty 'wrappers' are for? Also how about the module which contains the function code itself. Where do I put this/ in what form i.e. compiled/source. How does the header file know where the functions it's defining are?
    LVL 45

    Expert Comment


    The header doesn't know where the functions are.  They don't care.  It's the responsibility of the programmer to make sure that any function that needs the headers (including the functions defined by the headers) #include the headers to define the prototypes.

    The module that contains the source for the actual executable is usually a file that ends with ".c".  Sometimes you put several functions into one source program, sometimes you put only one function, depending on your needs.  Usually, you group related functions that have a common code base.  #include <stdlib.h> defines the descriptors for many dozens of functions, but the executable code is split into numerous modules.

    The wrappers prevent the definitions from being repeated, even if you #include <MyFile.h> several times.  This way you can #include <MyFile.h> in your source program to define the objects.  You might need other headers files that also rely on #include <MyFile.h>.  The wrappers allow you to put the #include statement everywhere that you need the definition.  C will expand it only once.


    Author Comment

    Thanks Kent, much appreciated

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now