home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18259 < prev    next >
Encoding:
Text File  |  1992-12-12  |  2.5 KB  |  57 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Calling convention: C vs PASCAL
  5. Date: 12 Dec 1992 18:34:11 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  7. Lines: 45
  8. Message-ID: <27947@dog.ee.lbl.gov>
  9. References: <1992Dec11.225756.26780@eng.ufl.edu> <1992Dec12.171137.17577@taumet.com>
  10. NNTP-Posting-Host: 128.3.112.15
  11.  
  12. In article <1992Dec12.171137.17577@taumet.com> steve@taumet.com
  13. (Steve Clamage) writes:
  14. >Neither the C nor Pascal language specifications require the parameter
  15. >ordering mentioned, but they are common implementation techniques.
  16.  
  17. (For that matter, these days it is common to pass the first two-to-six
  18. parameters in registers, except on the 80x86 where the registers are
  19. far too precious to be used this way.)
  20.  
  21. >C allows a function to be called with more parameters than were declared
  22. >for it.
  23.  
  24. In ANSI C, this is only true of `, ...' functions, such as printf().
  25. Moreover, these must be declared in advance as such.  This gives IBM PC
  26. C compilers license to use the variant of `return' that adds to the
  27. stack pointer, except when calling or returning from one of these
  28. `special' functions.
  29.  
  30. In particular, this means that:
  31.  
  32.     /* incorrect version of `hello world' */
  33.     int main(void) { printf("Hello world\n"); return 0; }
  34.  
  35. may well malfunction on an IBM PC.  To correct it, you *must* add at
  36. least one of the following two lines:
  37.  
  38.     #include <stdio.h>
  39.     int printf(char *, ...);
  40.  
  41. It is then guaranteed to work on any ANSI-conformant hosted system,
  42. including all IBM PC C compilers.  Without the `advance notice' that
  43. printf() is a `special' function, it need not work properly.
  44.  
  45. Of course, if one happened to go to your nearby mall and buy an IBM PC
  46. C compiler, and it failed to compile and run the incorrect version, one
  47. would be tempted to advise others against buying this particular
  48. compiler---not necessarily believing that it is `wrong' or `buggy', but
  49. perhaps only because one knows just how much old, nonconformant code
  50. exists.  In other words, market forces pretty much guarantee that *your*
  51. IBM PC C compiler by default uses the so-called `slow method' (which,
  52. as Steve pointed out, need not actually be slower: there is no reason
  53. that one *must* clear the stack after every function call).
  54. -- 
  55. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  56. Berkeley, CA        Domain:    torek@ee.lbl.gov
  57.