Reputation: 5491
I have a header foo.h
file that declares a function prototype
void foo(FILE *f);
/* ... Other things that don't depend on FILE ... */
among other things.
Now obviously, to use this header, I need to do the following
#include <stdio.h>
#include "foo.h"
I would like to surround this particular prototype with something like the following:
#ifdef _STDIO_H
void foo(FILE *f);
#endif
/* ... Other things that don't depend on FILE ... */
so that I can #include "foo.h"
without worrying about #include <stdio.h>
in cases where I don't need that particular function.
Is the #ifdef _STDIO_H
the way to go if I want my code to be portable and standards compliant?
I could find no mention of _STDIO_H
in the standards document, but I see it is used in a variety of C libraries. Should I rather use something that I know to be defined in stdio.h
, like EOF
?
A related question: What do you do for other standard C headers, like stdlib.h
?
Upvotes: 3
Views: 2459
Reputation: 1
<stdio.h>
and <stdlib.h>
are part of the C99 (and C11) standards. So every (hosted) standard conforming C implementation have them.
On most practical implementations, they are header files with some include guards.
A standard conforming implementation might process #include <stdio.h>
very specifically, e.g. by using some database. I know no such implementation.
So simply add
#include <stdio.h>
near the top of your header file, something like
// file foo.h
#ifndef FOO_INCLUDED
#define FOO_INCLUDED
#include <stdio.h>
// other includes ...
// ...
// other stuff
#endif /* FOO_INCLUDED */
Alternatively, you could not care and document that #include "foo.h"
requires a previous #include <stdio.h>
; any sensible developer using a good-enough C implementation would be able to take care of that.
Actually, I was wrong in my comment on Alter Mann's deleted answer. It looks like stdin
is required to be some macro, and then you might use #ifdef stdin
... endif
as Alter Mann correctly answered. I believe it is not very readable, and you just want to have <stdio.h>
included, either by including it yourself in your foo.h
or by requiring it in your documentation.
Contrarily to C++ standard headers, C standard headers are in practice quite quick to be compiled, so I don't think it is worth to optimize the unusual case when <stdio.h>
has not been included.
Upvotes: 1
Reputation: 6573
Open your stdio.h
file (for your compiler) and see whether it has _STDIO_H or similar definition.
Upvotes: 0