nalzok
nalzok

Reputation: 16107

Writing prototypes instead of #include <stdio.h>

For example, here is a "hello, world" program without stdio.h included:

int puts(const char *str);

int main(void)
{
    puts("hello, world");
}

I even think this can be a good programming style when my program gets longer and longer, for all functions called are listed at the beginning explicitly.

So my question is: Besides providing prototypes for standard library functions, what else does #include <stdio.h> do?

Upvotes: 0

Views: 234

Answers (3)

Lundin
Lundin

Reputation: 213832

When using proper program design, all prototypes of public functions are placed in header files and all function definitions are placed in c files. This is how you write C programs, period.

It is the industry de facto standard way of C programming and no professionals use any other design.

Your personal preference is not relevant here, nor are any loop-holes in the C standard that would allow you to make a different design. You should write your C programs in the same way as the rest of the world does.

Upvotes: 1

The (non-normative) Appendix J.2 of the C11 standard draft lists the following among examples of undefined behaviour:

— A function, object, type, or macro that is specified as being declared or defined by some standard header is used before any header that declares or defines it is included (7.1.2)

However, as pointed out by Keith Thompson, the 7.1.4p2 says:

2 Provided that a library function can be declared without reference to any type defined in a header, it is also permissible to declare the function and use it without including its associated header.

Thus using puts without including <stdio.h> can indeed be done in standard-conforming manner. However, you cannot declare fputs, since it requires a pointer-to-FILE as an argument, which you cannot do in a strictly conforming manner.

In addition, puts might also be a macro in presence of <stdio.h> and expand to something faster in the presence of the header.

All in all, the number of functions that can be declared properly without including headers is not that large. As for the functions that use some types from the headers - if you're asking with language-lawyer tag about C, the answer comes from the standard and the standard is rather outspoken about this: don't do it, or your program will not be strictly conforming, period.

Upvotes: 7

Keith Thompson
Keith Thompson

Reputation: 263257

<stdio.h> defines the type FILE, among other things. You can't portably call any function that takes a FILE* parameter or returns a FILE* result without #include <stdio.h>.

And there's really no good reason to declare any of the functions yourself rather than including the header.

Upvotes: 5

Related Questions