Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
In C we include header files in the top of the program. But the actual logic is written in the libraries. My query is why can't we write:
#include<math.lib>


instead of:
#include<math.h>


As far as i know a declared function is of no use to me unless and until i give it a body in .h file I have the declaration part but the actual logic is written in my .lib file so why not to include the lib files directly.

Please help me out
Thanks & Regards
Radix :rose:
Posted
Updated 16-Aug-10 3:06am
v4

In C you don't need to include headers. C (and C++) headers are just a handy place to bung declarations that define the interface to a particular lump of code. They (should) contain the minimum amount of information you need to use a particular object file or library.

Say I'm writing a hello world. Instead of the traditional way I can write:

C
int main( int, const char ** )
{
    int printf( const char *format, ... );
    printf( "Hello world!!\n" );
}


and it'll all work swimmingly (on a standard implementation anyway).

The idea behind it is to keep the tools simple. To generate a call to printf the compiler just needs to know what its interface is. If the compiler knows that it can generate a call to the function after setting up the parameters and clean up the stack when the function returns it doesn't have to know anymore about how printf is implemented.

It also keeps the library and object file format simple as well. With this 1970s compile/link model object files are just blobs of binary data with a table of names mapped to offsets into the blob. If you want a more sophisticated model there's no reason you couldn't implement one but you'd pile more stuff into the object file and end up with something like a DLL or Java JAR rather than an object file.

So there are really two main reasons:

- inertia (or tradition) - it's been with us since 1965 or so (Fortran uses a similar model)

- pragmatism - makes it easier to link all sorts of stuff together, including assembly languages to high level languages.

Cheers,

Ash
 
Share this answer
 
Header file contain declarations written in C++ while .lib contains assembly. It is not very nice to ask compiler to parse assembly files for symbol checking. Do you understand the difference between compiler and linker?
 
Share this answer
 
Comments
radix3 16-Aug-10 9:07am    
yes i do understand the difference, but c++ was introduced later c came first and i forgot to mention i am using turbo c++ editor, so how can u say that header files are written in c++ ?????????????
Nyarost 16-Aug-10 9:13am    
Correct, my error. They are written in C. But this doesn't change the things. #include directive simply puts the contents of included file into into file where it is used. I can use #include . As a result I'll get large amount of binary data in my source file. Header files are used by preprocessor and allow one declaration for multiple files. While .lib fails contain native assembly. So .lib file are at least not portable. Header files are.
radix3 16-Aug-10 9:16am    
Also i have opened a header file math.h but i didn't find any of the c++ part so please justify your answer, and as far as i know compiler will only check the syntax that's all and that to the file which you are talking about is the .i file which is given to the compiler after the pre-processing part so the .i file is converted into .obj file and then .obj is converted into .exe by the linker. Please help me out regarding my query. Thank you
radix3 16-Aug-10 9:22am    
But whats the use of getting a large amount of binary data where i just want to use only two functions that's printf and scanf so in that case all the other files which are included into my source code are useless for me as i am using only these two functions, so in this case i can say that unnecessary data is being added into my code file which is of no use
Nyarost 16-Aug-10 9:24am    
Imagine that there is .i file gained after preprocessing stage. it contains both source code and binary data from .lib file. What compiler should do with this? It is not compiler's task to parse binary libraries. It creates object files. Linker searches in the .lib files for symbols (functions, variables, constants etc) that aren't located in object file.
Also .h files allow not to duplicate code. Imagine we have compiler that dissassembles .lib files. I have 2 files that use math.lib. So code is being duplicated. What to do? Which function to use? I'll have to write something like

#ifndef math_lib_included
    #define math_lib_included
    #include <math.lib>
#endif

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900