• C

Create own C header file

   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,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
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,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dustybrynAuthor Commented:
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?
Kent OlsenData Warehouse Architect / DBACommented:

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.

dustybrynAuthor Commented:
Thanks Kent, much appreciated
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.