Frage

Wenn ich Folgendes habe:

/*
 * example.h
 */

#ifndef EXAMPLE
#define EXAMPLE

#include <stdio.h>

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

#endif


Wird dies diesen Code bedeuten, der ... #include example.h ... muss nicht ... #include example.h'S ... Abhängigkeiten? (dh: #include <stdio.h> )

War es hilfreich?

Lösung

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

Andere Tipps

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.

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top