[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

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,

  • 2
  • 2
1 Solution
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,
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now