开发者

Is it necessary to write these headers in C?

I want a list of Header files in C which are not necessary to use them.

Example:

scanf(), printf(),.... etc. //can be use without stdio.h
getch()...etc.   //can be used without conio.h

Is is necessary to write these headers(stdio.h, conio.h) while I us开发者_Go百科e these(above) methods?


Using functions without prototypes is deprecated by the current standard, C99, and will probably be removed in the next version. And this is for a very good reason. Such usage is much error prone and leads to hard to track faults. Don't do that.


In the current C language standard, function declarations (but not prototypes) are mandatory, and prototypes have always been mandatory for variadic functions like printf. However, you are not required to include the headers; you're free to declare/prototype the functions yourself as long as you have the required types available. For example, with printf, you could do:

int printf(const char *, ...);
printf("%d\n", 1);

But with snprintf, you would need at least stddef.h to get size_t:

#include <stddef.h>
int snprintf(char *, size_t, const char *, ...);

And with non-variadic functions, a non-prototype declaration is valid:

int atoi();


Depends on your compiler.

For GCC: http://gcc.gnu.org/viewcvs/trunk/gcc/builtins.def?view=markup


Basically you can use any C function without a header. The header contains the prototypes for these functions making it possible to warn about wrong type or number of parameters.

So yes you can do without these headers.

But no you shouldn't do this.

But normally you don't write these header, these are provided by the build environment.

Another thing since these checks are only warnings in C you should switch on these warnings and treat them like errors. Otherwise you are in for a very bad C experience.

In gcc you should always run with options -W -Walland avoid all warnings these give.

An BTW these are not methods but functions.

Addendum: since you are gonna treat all warnings as errors you might turn on -Werror which turns all warnings into error just enforcing this.

Personally I'm not using this option but have the discipline to clean out all warnings in the end. This makes it possible to ignore the warnings for a while and usually do a clean up before I commit to version control.

But certainly for groups it makes sense to enforce this with -Werror e.g. in test scripts run before allowing commits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜