Click to See Complete Forum and Search --> : Multiple header inclusions and the C preprocessor


Concrete Geist
10-20-2001, 12:08 AM
I need a better understanding of how the C preprocessor is used to prevent multiply header file inclusions. Here is an example of my book, which I can't figure out.


#if defined( prog_h )
/* The file has been included already */
#else
#define prog_h

/* Header file goes here... */

#endif


Now say I write a header file that requires string.h and ctype.h, how would I avoid multiple inclusions in the main program by the programmer? The above example doesn't explain this to me.

[ 20 October 2001: Message edited by: Concrete Geist ]

jemfinch
10-20-2001, 12:19 AM
Every include file in C should have something like this:


#ifndef STRING_H
#define STRING_H
...
#endif


So it can't be included multiple times. In a .c file, you should never have to
worry about this.

Jeremy

tnordloh
10-20-2001, 08:55 AM
Think of .h files as 'perfect code'. They already have appropriate #ifdef and #ifndef statements, meaning you don't have to worry about them. I recommmend trying out if you want. include like 20 copies of stdio.h and see if your compiler wigs out : ). That way you can learn to trust the compiler, which was a big issue for me.