Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make header files in C/C++

Status
Not open for further replies.

LauroBezerra

Programmer
Sep 11, 2002
6
0
0
BR
Hello, guys!!!

Can somebody help me how to make header files in C/C++?
I have some books but they dont show me how.
I need some simple examples.

Please, help-me!!
 
hello,

i can't make sure what you just need. head file in C is .h file, normal you should write what you included file at the top of file. just as following:

#include <stdio.h> - standard heasd file
#include &quot;myConfig.h&quot; - your personal file

and the normal is head file of tws:

#ifndef __TWS_H__
#define __TWS_H__

#define MAX 5
typedef struct _Tws TWS;
struct TWS
{
int sp1;
int sp2;
int data[MAX];
};

/*command routine*/

int IniStack (TWS *tws);
int Push(TWS *tws, unsigned i, int data);
int Pop (TWS *tws, unsigned i, int *pdata);
void Dump (TWS *tws);

#endif /*end of __TWS_H__*/
 
here is a litle example

hello.h
----------
Code:
#if !defined __HELLO_H
#defined __HELLO_H      /* define hello.h */

#include <stdio.h>      /* for printf() */

void hello(); /* prototype of function */

/* this function exicute u calls hello() in ur .C file */
void hello()
{
    printf(&quot;Here i'm saying the worlds most popular words.&quot;);
    printf(&quot;\nHello World!&quot;);
}
#endif    /* end of hello.h */

hello.c
----------
Code:
#include &quot;hello.h&quot;

int main()
{
    hello();
    return 0;
}

output
---------
Here i'm saying the worlds most popular words.
Hello World!
 
The interface goes in func.h - typedefs, enums, #defines, function prototypes, extern vars

The implementation goes in func.c - function definitions, global data, static data and static functions.

Anything which needs to use func includes func.h

For a compiler, you would do
gcc main.c func.c

For an IDE, add main.c and func.c to the project
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top