C: Header Inclusions

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

      


Does this mean that code that uses ... #include example.h

... shouldn't ... #include example.h

... dependencies? (i.e .: #include <stdio.h>

)

+2


source to share


3 answers


That's right - that's why its good practice to limit the required headings to be included in other headings. The preprocessor will replace the "#include" directive with the contents of stdio.h, so your header will look 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

      

+1


source


Yes

But if the code depends on <stdio>

, it should probably include it. (After all, it <stdio>

also has multiple include protections.)



If what you want is a wizard for your project, then do it, but include both actual headers and general system headers in it, but not prototypes or declarations or macros. That is, mega include does nothing except include. Thus, individual modules can make their own decisions in accordance with the development of the program.

+2


source


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

For your specific example, you need the FILE

one declared in <stdio.h>, so it's good to include it.

If the prototype parse_string () used size_t

instead unsigned int

and const char *

(for filename) instead FILE *

, I would include <stddef.h> in "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

      

0


source







All Articles