When do you #include <stdlib.h>?

brian at bucc2.UUCP brian at bucc2.UUCP
Thu Mar 24 13:54:00 AEST 1988


> /* ---------- "When do you #include <stdlib.h>?" ---------- */
> 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 prototype for atol() resides in stdlib.h.  The above code compiles
> without an #include <stdlib.h>, but the expresion yields rubbish.  When
> the stdlib.h is included, it works fine.  What gives?  Why doesn't the 
> compiler barf when it encounters atol() when the stdlib.h is NOT included?

  Since you didn't tell it otherwise, your compiler assumed that atol()
returned an integer. It actually returned a long integer, and since your
program produces garbage output I gather sizeof(int) != sizeof(long)
under your implementation.

  If you didn't want to include <stdlib>, you could of done the prototype
yourself:

long atol();

  or if you are blessed with an ANSI compiler

long atol(char *str);

...............................................................................

  When the going gets weird, the weird turn pro.

  Brian Michael Wendt       UUCP: {cepu,ihnp4,uiucdcs,noao}!bradley!brian
  Bradley University        ARPA: cepu!bradley!brian at seas.ucla.edu
  (309) 691-5175            ICBM: 40 40' N  89 34' W



More information about the Comp.lang.c mailing list