murillo@sigi.Colorado.EDU (Rodrigo Murillo) (03/22/88)
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 murillo@boulder.colorado.edu | ..{hao|nbires}!boulder!murillo ( Machines have less problems. I'd like to be a machine. -- Andy Worhol )
gwyn@brl-smoke.ARPA (Doug Gwyn ) (03/23/88)
In article <4991@sigi.Colorado.EDU> murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes: > 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().
karl@haddock.ISC.COM (Karl Heuer) (03/23/88)
In article <4991@sigi.Colorado.EDU> murillo@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@haddock.isc.com), The Walking Lint
brian@bucc2.UUCP (03/24/88)
> /* ---------- "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@seas.ucla.edu (309) 691-5175 ICBM: 40 40' N 89 34' W
swh@hpsmtc1.HP.COM (Steve Harrold) (03/29/88)
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.
mouse@mcgill-vision.UUCP (der Mouse) (04/09/88)
>> 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. > 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 uucp: mouse@mcgill-vision.uucp arpa: mouse@larry.mcrcim.mcgill.edu
gwyn@brl-smoke.ARPA (Doug Gwyn ) (04/10/88)
In article <1035@mcgill-vision.UUCP> mouse@mcgill-vision.UUCP (der Mouse) writes:
-> 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.