how to create .h header files for .c files? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to create .h header files for .c files?

jennafjennaf Posts: 1Member

Hi, I have a program that I just wrote, but really its just a program that will be used by a larger program that I am working on. The larger program will run some functions in the small program, accessing some of its global arrays/variables to extract information.

I never knew how to create a header file so that this large program can include it. I think what i use to do was just "include small_prog.c" but I know thats not correct.

So how do we go about creating a header file?

The small program basically has:
-1 global array variable pointer
-3-4 int variables
- includes math.h, time.h, stdio.h, stdlib.h ctype.h
- #define MAX_ARRAY_SIZE 100
- a main function (only used if your running the file on its own)
- several functions that perform instructions to modify the above variables.

an example of this could be:
----------------------------------
small_prog.c

#include
#include
#include
#include
#include


#define MAX_ARRAY_LENGTH 100
int* matrix_array;
int dimension=0;
int matrix_mode=0;
int x = 0;

void func1 (int x);
float func2 (float y);
int func3 (char a);

int main(int argc, char* argv[])
{
....
.
.
and so on
----------------------------------

So I want to include this small program in a large program, by refering to a .h file. but what do i write in the h file?
Jenna

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Header file contains function prototypes, structure declarations, constant definitions, like #define.[/color]
    [code]
    //
    // MODULE1.H FILE:
    //
    void func1 (int x);
    float func2 (float y);
    int func3 (char a);
    [/code]
    [color=Blue]Source file contains function bodies. It is preferred that names of both files are same, but they have different extension, like MODULE1.H and MODULE1.C:[/color]
    [code]
    //
    // MODULE1.C FILE:
    //
    #include "MODULE1.H"

    void func1 (int x)
    {
    // ... your implementation here ...
    }

    float func2 (float y)
    {
    // ... your implementation here ...
    }

    int func3 (char a)
    {
    // ... your implementation here ...
    }
    [/code]

    [color=Blue]After these TWO files are done they are used in a project - or you call it a large program. Usually, the development environment which you use has the ability to insert files into project. You need to insert both files into projects and then just "#include ".h"" into other C files of the project where you need those functions.

    If you do not have an environment (like Dev-C++ or Visual C++ or something else...) then "include .c" is the way to go.

    There are few other ways: like to compile the C file into .OBJ file and then use LINK tool to build the full program. It is usually done by the environment, but you can also do it manually - using command line tools.
    [/color]
  • lutzhelllutzhell Posts: 6Member
    And don't forget to put your header code between [link=http://en.wikipedia.org/wiki/Include_guard]include guards[/link] to avoid problems with multiple inclusions.

    Like this:

    module1.h:
    [code]
    #ifndef MODULE1_H_
    #define MODULE1_H_

    //
    // MODULE1.H FILE:
    //
    void func1 (int x);
    float func2 (float y);
    int func3 (char a);

    #endif // MODULE1_H_

    [/code]
  • srinathReddyDsrinathReddyD Posts: 4Member
    You can find the complete details and the tutorial on creating header files in c from the below link

    [link=http://www.beginners-tutorials.com/2012/05/how-to-create-our-own-header-files-in-c.html]How To Create Header And Library Files in C[/link]

    Regards:
    Srinath Reddy.
    http://www.beginners-tutorials.com
  • srinathReddyDsrinathReddyD Posts: 4Member
    check the below link for complete tutorial on creating header files in c.

    [link=http://www.beginners-tutorials.com/2012/05/how-to-create-our-own-header-files-in-c.html]How To Create Header And Library Files in C[/link]
    Regards:
    D.srinath Reddy
    Admin
    http://www.beginners-tutorials.com
Sign In or Register to comment.