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 Member Posts: 1

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 Member Posts: 6,519
    [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 Member Posts: 6
    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 Member Posts: 4
    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 Member Posts: 4
    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
  • tushars1994tushars1994 PuneMember Posts: 1

    It is very simple. Follow the steps below:
    1. Create a .h file.
    2. Create a .c file and include the header file in it.
    Find more about how to create own header files in C Programming here: C Program To Create Your Own Header Files
    This is the sample:

    include<stdio.h>

    include"newheaderfile.h"

    void main()
    {
    printf("Sum:\t", sum(10,20));
    }

  • webiletechnologieswebiletechnologies IndiaMember Posts: 3

    A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

    You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.

    Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.

    A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required.

Sign In or Register to comment.