You are on page 1of 3

About printf() function Having learned about functions in the language of C, I remember thinking: I need to start looking for

the printf() function and see how it looks like. But all that I would find were many files ending in .h .a and .o. Nowhere there was a function named printf. In fact, I couldn't find any code of the making of any of the standard functions that C language has. The reason of this mystery lays behind the scene of the implementation of these functions. They are converted to object code or binary blocks ending in .o or .a, in the case of libraries. The actual implementation might even be written in Pascal, Fortran, Assembly, Delphi, etc. All that you get in the compiler directories are these binaries files, and what is called header files, ending in .h, which are nothing more that an interface to those functions. Allowing you to know how to use the functions, by indicating the prototype of it. Meaning, what it returns, and the parameters that would accept. As an exercise you could create your own library to loosely imitate the above concept. Suppose that we want to create our own implementation of the standard function strcat(). A possible solution could be:
Code:

/* * stringCat.c * implementation of the strcat function * Aia, Jan 27, 2008 */ char *stringCat( char *strOne, const char *strTwo ) { char *ptr = strOne; while( *strOne ) { ++strOne; } while( (*strOne = *strTwo) ) { ++strOne, ++strTwo; } return ptr; }

Now we create a header file so the programmer user can make use of it.
Code:

/* * stringCat.h * * stringCat function: * parameters: * char* recipient * const char* to append * returns a pointer to the recipient string */ char *stringCat( char *, const char * ); Now let's start creating the static library. I am using the gcc compiler and the command line for that is: gcc -c stringCat.c -o stringCat.o This will create a stringCat.o file. The command: ar rcs libstringCat.a stringCat.o will archive that object file into a library file ended in .a And we're done. All that is required in order to use that function in the library is to write a piece of code:
Code:

/* * stringCatTest.c * implementation of the strcat function */ #include <stdio.h> #include "stringCat.h" /* user made static library stringCat, header file reside in the current directory */ int main ( void ) { char sOne[20] = "Hello"; char sTwo[] = " World"; stringCat( sOne, sTwo ); printf( sOne ); return 0; } and compile that program linking it with the library. I do it like: gcc -static stringCatTest.c -L. -lstringcat -o stringCatTest.exe Notice the (.) after the -L Run the program, and you'll find out if your own function is...well, functioning

Check this link for some information http://www.koders.com/c/fid4E0FA789459803C1E4369279B96E5C9B257941C8.aspx?s=pri ntf

Actuall this data from this link

http://cboard.cprogramming.com/c-programming/98344-header-files.html#post710554

You might also like