When do you #include <stdlib.h>? 
Author Message
 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?

--
_______________________________________________________________________________
 Rodrigo Murillo, University of Colorado - Boulder  (303) 761-0410

 ( Machines have less problems.  I'd like to be a machine. -- Andy Worhol )



Mon, 17 Aug 1992 00:18:00 GMT  
 When do you #include <stdlib.h>?

Quote:

>     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.

Naturally, since the compiler assumes that the return type of a function
is int if it is invoked with no declaration in scope.  Since the correct
return type for atol() is long, the default assumption is incorrect and
the wrong format of data is passed as an argument to printf().


Mon, 17 Aug 1992 18:53:00 GMT  
 When do you #include <stdlib.h>?

Quote:

>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.

Quote:
>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".




Mon, 17 Aug 1992 23:38:00 GMT  
 When do you #include <stdlib.h>?

Quote:
> /* ---------- "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

  (309) 691-5175            ICBM: 40 40' N  89 34' W



Mon, 17 Aug 1992 03:54:00 GMT  
 When do you #include <stdlib.h>?
Re: When to #include anything

Does anyone have knowledge of, or even better, code to share, that will
scan a C source file, determine all the function calls embedded within it,
and produce as output a list of files that should be included?

This would simplify greatly the task of getting the minimal set of includes
needed to make good use of the ANSI function prototyping capability.

An additional output that would be useful is to print a list of the order
the functions in the file should be placed, so that they are defined before
their first use in the file.  Once again, the purpose is give the compiler
prototyping information.  I know I can do this by trial and error (by letting
the compiler trip on mismatches if I get something wrong), but a tool would
be so much cleaner.



Mon, 17 Aug 1992 16:07:00 GMT  
 When do you #include <stdlib.h>?

Quote:
>> I realized today that you can call functions that are in stdlib.h
>> without actually including [stdlib.h].

Sometimes.  Those that are real functions, maybe.

Quote:
> If you didn't want to include <stdlib>, you could [have] done the
> prototype yourself:
> long atol();
>   or if you are blessed with an ANSI compiler
> long atol(char *str);

Unless, of course, what stdlib.h actually says is

#define atol(s) _builtin_atol(s)

                                        der Mouse





Thu, 17 Sep 1992 21:31:00 GMT  
 When do you #include <stdlib.h>?
-> If you didn't want to include <stdlib>, you could [have] done the
-> prototype yourself:
-> long atol();
->   or if you are blessed with an ANSI compiler
-> long atol(char *str);
-Unless, of course, what stdlib.h actually says is
-#define atol(s) _builtin_atol(s)

No, there must also be an actual atol() function in the ANSI C library.



Thu, 17 Sep 1992 22:56:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <<<>>>Need C code advice with functions and sorting.<<<>>>

4. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

5. Problems with strtol() from <stdlib.h>

6. book about <stdlib.h>

7. Cannot find <stdlib>

8. >>>Windows Service<<<

9. What is the use of <stdlib.h> and <math.h>

10. proposal: <basic.h>, <pascal.h>, <fortran.h>, <cobol.h>

11. <<<< C Grammar for yacc needed >>>>

12. - - - = = =<><> W e b - U S A <><>= = = - - -

 

 
Powered by phpBB® Forum Software