Sunday, August 2, 2009

What is the terminology 'include', 'library' in C programming?

What is the meaning of library, include in C programming?


any example show?

What is the terminology 'include', 'library' in C programming?
Includes are used by the compiler's pre-processor to "include" a header file. This file will typically have constants and function definitions.





Example


------------------------





#include %26lt;stdio.h%26gt;


int main (){


FILE *myfile;


myfile = fopen ( "somefile", "w+" );


fprintf ( myfile, "Hello File!\n" );


fclose ( myfile );


return 0;


}





--------------------------------





fprintf is a function that's defined in stdio.h. If you don't include the header file, the compiler will have no idea how to properly handle the function call.





The actual code for fprintf is in a library. Specifically the Standard C library. This library is fairly large and contains the code for common functions.





You don't tell the compiler about the library. You tell it about the include file. It will generate code with a "stub" for fprintf. Then when you use the linker, you tell the linker about the library. The linker will the replace the stub with the actual code for fprintf. Most modern compilers appear to perform the compile and link in one command, but it's really two separate steps.














( Note: this is an over simplification and doesn't cover dynamic linking , and the example code is a horrible example of proper error handling .)
Reply:An include statement allows a c program to use another chunk of code from another file.





For example, in C, you usually have to include the header file stdio.h which contains many of the common primative functions that are used in the normal course of C programming.





A simple example would be an include file which would contain an add function that would return the sum of two integers.





add.h





int add2ints(int,int);


int add2ints(int x, inty){


return (x+y);


}





mainprogram.c





#include %26lt;stdio.h%26gt;


#include %26lt;add.h%26gt;





int main(void){


int x=3;


int y=2;


int result=add(2,3);


printf(result);


}


No comments:

Post a Comment