سؤال

إذا كان لدي ما يلي:

/*
 * example.h
 */

#ifndef EXAMPLE
#define EXAMPLE

#include <stdio.h>

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

#endif


هل يعني هذا أن الرمز الذي يستخدم ... #include example.h ... لن تضطر إلى ... #include example.hتبعيات ... ( بمعنى آخر: #include <stdio.h> )

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top