470,815 Members | 1,221 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

how to create a header file in c please explain by defining simple function?

please tell me how ro define a header file for a perticular function so i can use in another file
please explain by giving example of any simple function suppose like addition of number just wanna know the basic concept.
thank you
Jul 18 '10 #1
1 1573
9,208 Expert Mod 8TB
Supppose you have this function in one file:

Expand|Select|Wrap|Line Numbers
  1. void Hello()
  2. {
  3.    printf("Hello\n");
  4. }
and you need to call it in another file:

Expand|Select|Wrap|Line Numbers
  1. Hello();
In this case the compiler will produce an error that the Hello function does not exist. You know it does exist in the other file, so you tell the compiler that there is a Hello() function but it's no in this file:

Expand|Select|Wrap|Line Numbers
  1. void Hello();
  2. Hello();
The function prototype tells the compiler not to worry about the missing Hello() function. The linker will find it.

Now you have a choice: a) write "void Hello();" in all 5000 of your source files or b) write "void Hello() once in a file by itself and include that file in all 5000 source files.

If you choose (b), then in one file you write:

Expand|Select|Wrap|Line Numbers
  1. MyStuff.txt
  3. void Hello();" 
And include this file in your other source files:

Expand|Select|Wrap|Line Numbers
  1. #include <MyStuff.txt>
  2. Hello();
Since these files are included at the top (or head) of the source files, they are called header files. By convention in C, they have a .h extension. In C++, they have no extension:

Expand|Select|Wrap|Line Numbers
  1. C
  2. #include <MyStuff.h>
  3. Hello();
  5. C++
  6. #include <MyStuff>
  7. Hello();
Jul 18 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by alan | last post: by
3 posts views Thread by Sujan Datta | last post: by
2 posts views Thread by free2klim | last post: by
4 posts views Thread by Jeffrey Spoon | last post: by
5 posts views Thread by Pramod | last post: by
6 posts views Thread by Ramon | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.