By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

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

P: 96
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
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
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
  2.  
  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();
  4.  
  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.