When do you #include <stdlib.h>?

Karl Heuer karl at haddock.ISC.COM
Wed Mar 23 09:38:34 AEST 1988


In article <4991 at sigi.Colorado.EDU> murillo at boulder.Colorado.EDU (Rodrigo Murillo) writes:
>I realized today that you can call functions that are in stdlib.h
>without actually including them in your file.  But on the otherhand
>you can include them.  Is there a hard fast rule?  Let me give an
>example:
>     printf("foo: %dl",atol(argv[1]));

The rule is that you are allowed to omit the #include, provided that you
supply a compatible declaration yourself.  Thus, you could have said
"extern long atol(char *);" or "extern long atol();" and your program would
have been correct.  Since nothing explicit was encountered, the implied
declaration was "extern int atol();" which was incorrect.

>Why doesn't the compiler barf when it encounters atol() when the stdlib.h is
>NOT included?

Absence of a prototype has to be permitted for compatibility with pre-ANSI C.
The better compilers will have an option to generate a warning, though.

Btw, that should be "%ld", not "%dl".

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list