views:

98

answers:

3

If I have the following:

/*
 * example.h
 */

#ifndef EXAMPLE
#define EXAMPLE

#include <stdio.h>

extern int parse_string(FILE *, char const*, const unsigned int);

#endif


Will this mean that code that uses ... #include example.h ... won't have to ... #include example.h's ... dependencies ? ( ie: #include <stdio.h> )

+1  A: 

Correct - this is why its good practice to limit necessary headers included within other headers. The preprocessor will replace the "#include" directive with the contents of stdio.h, so your header will look something like this to the compiler:

/*
 * example.h
 */

#ifndef EXAMPLE
#define EXAMPLE

<contents of stdio.h>

extern int parse_string(FILE *, char const*, const unsigned int);

#endif
Justicle
+2  A: 

Yes

But if the code depends on <stdio>, it probably should include it. (After all, <stdio> has multiple inclusion protection as well.)

If what you want is a master include for your project, then go ahead and make one, but have it include both the actual headers and the common system headers, but no prototypes or declarations or macros. That is, have the mega include do nothing but includes. This way individual modules can make their own decisions down the line as the program evolves.

DigitalRoss
A: 

Yes, you can do that and it will have the desired effect.

For your particular example you need FILE declared in <stdio.h>, so it's good to include it.

If the parse_string() prototype used size_t instead of unsigned int and const char * (for a filename) instead of FILE *, I'd include <stddef.h> in the "example.h"

#ifndef EXAMPLE
#define EXAMPLE
#include <stddef.h> /* size_t typedef */
extern int parse_string(const char *filename, char const*, const size_t);
#endif
pmg