Login Register






Creating And Using Header Files In C filter_list
Author
Message
Creating And Using Header Files In C #1
Alrite, in this thread, I'm going to teach you how to create and use header files in C programming..

What are header files??

In computer programming, header file is a file that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required. This is to keep the interface in the header separate from the implementation. The C standard library and C++ standard library traditionally declare their standard functions in header files.

The steps are as follows :
1) Type in the code.
2) Save the code with any suitable header file name and with .h extension instead of .cpp extension.
3) Compile the code.
4) Make another code and include your header file in it.
5) Use the functions of your sample header file.

Here's a simple example :


The code for the header file...

Code:
int fact(int n) { if(n==0) return 1; return n*fact(n-1); } int power(int a,int b) { int n,p=1; if(b==0) return 1; else { for(n=1;n<=b;n++) p=p*a; return p; } }

This code will create two functions i.e. power(int,int) and fact(int) which are both int return type. They return the power to and factorial of a number.

Save this file as header.h or any other name with the .h extension and then compile it.


The code for using and checking whether the header file works....

Code:
#include<stdio.h> #include<conio.h> #include"header.h"//the header file created by you is being called //Remember to use the "" for user defined header files and not <> void main() { int x=5,y=5,z=2; printf("%d",fact(x));//fact(int) function being called printf("\n%d^%d=%d",y,z,power(y,z));//power(int,int) function being called getch(); }

In this code, remember to use #include"header.h" instead of #include<header.h> if you have not created the header file in the BIN directory...

Here, I'd just created a simple header file to find find factorial and power to. You can include them in any program. You may create complex and hi-fi functions and use them...

Thats it...Cool
Enjoy...
[Image: 2YpkRjy.png]
PM me if you need help.
My pastebin HERE. My URL Shortener HERE.

Reply

RE: Creating And Using Header Files In C #2
Thanks man! You really helped me out on this one!
[Image: Signature.png]

Chek Out My Tutorials
| Set Up A Pentest Lab | How To Make Bitcoins |

Reply

RE: Creating And Using Header Files In C #3
I'm always there to help...

Reply

RE: Creating And Using Header Files In C #4
thnk u...........................................

Reply

RE: Creating And Using Header Files In C #5
(04-06-2012, 11:33 AM)krishanu Wrote: thnk u...........................................
You are always welcome...

Reply

RE: Creating And Using Header Files In C #6
it is usually have .cpp file for the definition file. e.g:

header.h
Code:
#ifndef __HEADER_GUARD__ #define __HEADER_GUARD__ //header guard #include <iostream> using namespace std; void first_funt (int, int); void second_funct (void); #endif

header.cpp
Code:
#include "header.h" void first_funct (int n, int m) { return (n*m); } void second_funct () { cout << "this is from second_funct()" << endl; }
[Image: ap8g35.jpg]

Reply

RE: Creating And Using Header Files In C #7
Thats another process...

Reply

RE: Creating And Using Header Files In C #8
what do you mean by "another process"?
[Image: ap8g35.jpg]

Reply

RE: Creating And Using Header Files In C #9
My post is for beginners... Your one is a bit advanced...

Reply

RE: Creating And Using Header Files In C #10
No, your post is not for beginners, nor his is advanced. Your post is simply wrong and it will fail to link ("compile") once you include that file in more than one other file. You cannot do headers in C/C++/many-similar-languages this way. Chipp's way is the correct and only way to do it.


And here's why (because I is important to support your statements with logical arguments):



When you use the #include "somefile.h" directive in a project, the preprocessor takes the contents of the somefile.h and places them right at the place, where is the directive specified. Much like you would just open the somefile.h, copied all text in it and placed it thread, instead of the #include.

Your header contains definitions of the function's bodies, so what happens if you include it in multiple files? The same function will be defined multiple times and that is illegal. You can define function only once (with an exception of templates and inline functions), however your "header" would be included multiple times in multiple files, therefore the functions would be redefined.

More on how headers and C++ compiling works - more detailed description why is your tutorial code wrong

The C++ compiling works in a way that makes it easier and faster to design things in a modular manner: you can have large project, consisting of many files, each one having many functions and/or classes. Now imagine that you wanted to compile that and every time, it would have to compile ALL of it, even if you changed just one file.

Fortunately, what C++ does is that it compiles each file that contains actual code (body of a function) into an object file, which contains the compiled instructions and some marks, that determine name and signature (parameters + return type) of the function. So if you have a .cpp file that has two functions and another file, that has two other functions, you can compile each one of them independently, resulting in two object files. They don't know about each other.

Let's imagine you're a compiler now.

But wait, if they don't know about each other, how am I going to call my function in another file? First, let's ask, what do you actually need to know to call a function. Do you need to know its body? No, you don't, you don't have to care how the function works inside to use it, you only need to care what information you have to give to the function and what data does it return back to you.

Why do you need that? Because the compiler needs to generate a machine code that will prepare data for the function - the parameters, followed by a call instruction - it's just a jump at another address in the memory, where is the function located. Then it lets the function do its work, it doesn't care what the code you jump to does, it just expects it will return back to location where you jumped from, so you can continue and possibly do something with the returned data.

So I guess it's pretty much clear that you don't need a function body to call it, you only need to know it's signature, or prototype, how C/C++ folks call it. Prototype is simply the function name, return type and parameters, for example:

Code:
int addition(int a, int b);


You include these prototypes in the header file and you can then use the function in all files, where you include the header, but you're not redefining the function, you're only saying, that such function exists and how it looks like. So whenever you use it, compiler just puts to the object file "hey, let's call function named "addition" that returns integer and takes two integer parameters".

Now you have another .cpp file, where the function body is defined, so the object file will contain data that say "following code is for function named "addition" that returns integer and takes two integer parameters".

So now you have a bunch of object files and you want to have a final executable file, so the linker is called. What it does, is simply taking all the compiled object files and putting them together. So it looks in one object file and finds a data that say "hey, let's call function named "addition" that returns integer and takes two integer parameters", so it begins to look in other object files for such function, until it finds it. So it calculates the address of the function body and puts it at the code, to finalize it.

Now, imagine we used your "headers" (those are sarcasm quotes by the way).

What would happen? The function would be defined an all files where you would include the "header" of yours, so there would be multiple object files that contain the body of the same function with same signature. So when the linker would find a call to such function, it would begin to search the object files (well it doesn't happen exactly like this, but for the sake of simplicity, it does) and it would find multiple definitions, so it would yell "OMG, which one should I use!?!?!" and die.

And that's why is your example wrong. If anyone has any questions about the innards and outtards of the C/C++ (and a lot of other stuff), feel free to ask me.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply