LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2008, 10:33 AM   #1
leedude
Member
 
Registered: Jan 2007
Location: Scotland
Distribution: Fedora, Debian
Posts: 81

Rep: Reputation: 15
header files....


hi, ive been investigating the uses of header files:
http://theoryx5.uwinnipeg.ca/programming...
the thing is, i cant see the point of them. at least not in this context.
if i remove the header file and all reference to it, i am still able to access the subroutine in my_subs.o as long as i compile it at the same time as the main program.
so why use a header file at all if it doesnt actually do anything?
 
Old 08-17-2008, 11:00 AM   #2
Phoxis
Member
 
Registered: Feb 2008
Location: India, Kolkata
Distribution: Fedora 16
Posts: 59

Rep: Reputation: 16
The header files contain different preprocessor directives, macros, function prototypes, typedef data and might also contain codes.
Now, when you use a macro it needs to be defined. When you call a function you need to define it, if defined after main you need to write a function portotype for that function before main. When you refer a typedef data, you need to declare it before its first use.
There are a lot of common libraries in programming languages (talking about C) All the inbuilt preprocessors,prototypes, typedefs are not possible to include by typing before the function main as thats a long list , like take the math.h which has the math functions #defines and function prototypes.
The function definitions of the function prototypes persent in the .h file are kept pre compiled inside a specific place depending of Operating System and the system configuration. This avoids redundant compiling of source codes of the whole library and reduces compilation time. Ater your program is compiled the linker links your object code with the precompiled object code of the library and make the executable.

When you #include a .h file before compilation of the program that file is opened and appended before the function main and then compiled with that long list of contents in the .h files which you have included. And the program knows the needed information of the stuffs which was defined, or declared in that .h file. It saves unnecessary typing a long listing data.

You can even create your own .h file of some abstract datatypes that you use and some functions which you made and would like to use frequently.
This avoids rewriting of the same codes.

Now somtimes under some IDE you need not include some header file to compile the program, i think because it automaticlly includes it.
As far as i remember in turbo C++ 2.0 you dont need to include stdio.h in order to get the IO function working. I dont know the actual cause.
But when you compile a program from command line you need to include them.

If anything is unclear please tell me.
If anything is wrong in the above code please correct.
 
Old 08-17-2008, 11:26 AM   #3
leedude
Member
 
Registered: Jan 2007
Location: Scotland
Distribution: Fedora, Debian
Posts: 81

Original Poster
Rep: Reputation: 15
thanks for the big reply.
its the function prototypes i dont understand - what are they for?
and how do they make the code behave differently to code without function prototypes in a header?
 
Old 08-17-2008, 11:34 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Function prototypes don't make code "behave differently". The point of them is to provide a definition of the function before use. The compiler will usually complain (probably not an error, but a warning) if you've used a function that hasn't been previously declared. See this.

Last edited by Nylex; 08-17-2008 at 11:36 AM.
 
Old 08-18-2008, 11:46 AM   #5
Phoxis
Member
 
Registered: Feb 2008
Location: India, Kolkata
Distribution: Fedora 16
Posts: 59

Rep: Reputation: 16
Quote:
Originally Posted by Nylex View Post
Function prototypes don't make code "behave differently". The point of them is to provide a definition of the function before use. The compiler will usually complain (probably not an error, but a warning) if you've used a function that hasn't been previously declared. See this.
Slight correction above. The function prototyping means "declarations" and not "definitions"
a function definition is when we program the function, that is write the code of the function.

A declaration is to notify the compiler that a function of the name with the parameters will be defined elsewhere in the program. Like

int my_function(int a,char b,float c);
or
int my_function(int ,char , float);

This is a function declaration (local or global depending on the location)
The above statements lets the compiler know that the function named "my_function" with the return type "int" and first argument "int" second argument "char" and third argument "float".
After we declare this the compiler knows the function identifier "my_function" and does not generate any error when it encounters it in the program in that function call. When we write the function ie define it elsewhere, the compiler gets it and checks that if the definition types and the declaration types match, if yes then okay , else it gives an error message.
If you pass an int in place of the float parameter that is pass some other parameters to the function when calling than it was described in the declaration then it gets you a warning of incomparable argument passing or similar.
Example

main()
{
int integer;
float floating_point;
char character_type;
....
...
my_function(floating_point,integer,character_type);
.
}

The above example passes float type data in place of integer and simmilar mixup is done.
Then the compiler will warn you.

So prototype gets you strong type checkups during compilation, so you can do a more organized programming, with lesser errors.
This need if felt when a lot of functions with many arguments are used and you need to keep care of all the parameters are passing of the correct type. Also in a multifile program each function cn stay in a single file. When compiling you need to write the protypes to let the compiler know the function names and its types to compile the program a file at once. This can be done with creating a .h file (header file) with all the prototypes and #including them. (Later linking the objects make a single executable)

In absence of the declaration the typechecks cannot be done, and you might accidentally pass an floating point value as a function argument where it actually was programmed to accept a pointer. Then your program will crash in runtime, and if the program is large you can overlook that small fault.

Prototyping also gets a list of functions used in a program. so if you are using a lot user defined function in a program then you have the full of functions which comes handy while programming and also developing. (personal view)

Please correct me if there is any wrong information.
Please add some more points of prototyping.
and Please ask if anything is still unclear.

Last edited by Phoxis; 08-18-2008 at 11:47 AM. Reason: Addition of information
 
Old 08-18-2008, 01:52 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by Phoxis View Post
Slight correction above. The function prototyping means "declarations" and not "definitions"
a function definition is when we program the function, that is write the code of the function.
Sorry, yes. I have a habit of getting the two mixed up.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files in C disruptive Programming 2 02-01-2006 07:36 AM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
gcc header files and cpp files? EchO Linux - Software 3 03-01-2005 01:14 AM
Header Files or Something Else ?? T-u-N-i-X Linux - General 0 12-18-2004 06:46 PM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration