Thread: Header Files

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    Question Header Files

    how and what is the language used to create header files?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Header files are just plain text files used to hold function prototypes, structs and other relevant information to be shared in multiple files in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Where would you find the actual function? Example: you'll find the prototype of printf() in stdio.h if you look for it (in Cygwin, it's in C:\cygwin\usr\include\) but where are the actual functions themselves?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but where are the actual functions themselves?
    Somewhere out there beneath the pale moonlight...

    Anyway, from what I understand, the implementation of printf() should be in the source code for the compiler, and the object code would be in say, libc or some other library file provided with the compiler.

    Likewise, the implementation of your own functions will be in your source code, separate from the header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Header files, besides C code, typically have what are called pre-processor directives. They start with a hash-symbol (#), like #define or #ifndef, #end, and have lots of uses.

    Todd

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A header is a file. You typically include the header files in the source files with pre-processor directives.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Quote Originally Posted by Abda92 View Post
    Where would you find the actual function? Example: you'll find the prototype of printf() in stdio.h if you look for it (in Cygwin, it's in C:\cygwin\usr\include\) but where are the actual functions themselves?
    take a look at this
    http://ftp.gnu.org/gnu/glibc/
    Code:
    # define EVERYTHINK 0
    int main ( void )
    { 
       while ( C_learning )
                 english_learning ( );
       return EVERYTHINK;
    }

  8. #8
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Varuna View Post
    how and what is the language used to create header files?
    Quote Originally Posted by Abda92 View Post
    Where would you find the actual function? Example: you'll find the prototype of printf() in stdio.h if you look for it (in Cygwin, it's in C:\cygwin\usr\include\) but where are the actual functions themselves?
    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
    Last edited by Aia; 01-27-2008 at 07:45 PM. Reason: adding bold letters

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM