Javed Akram
Javed Akram

Reputation: 15344

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 use these(above) methods?

Upvotes: 0

Views: 323

Answers (4)

Peer Stritzinger
Peer Stritzinger

Reputation: 8372

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.

Upvotes: 0

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215259

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();

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78903

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.

Upvotes: 4

Foo Bah
Foo Bah

Reputation: 26271

Depends on your compiler.

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

Upvotes: 0

Related Questions