home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13388 < prev    next >
Encoding:
Text File  |  1992-09-09  |  2.2 KB  |  53 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Portable Varargs?
  5. Message-ID: <1992Sep9.151730.27385@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Sep8.171222.20177@kpc.com>
  8. Date: Wed, 9 Sep 1992 15:17:30 GMT
  9. Lines: 42
  10.  
  11. hollasch@kpc.com (Steve Hollasch) writes:
  12.  
  13.  
  14. >    Is there a portable way to do variable argument functions that will
  15. >also work on ANSI C compilers as well as standard K&R C compilers?  Right
  16. >now it looks like varargs.h and the accompanying macros works on the
  17. >platforms I've tried, but was wondering if I can assume that this utility
  18. >will work on all "reasonable" platforms.
  19.  
  20. Variable-argument functions may be implemented in many different ways
  21. by compilers.  For this reason, you can't rely on any particular
  22. set of macro definitions to be portable, by which I mean you can't
  23. carry around your own varargs.h or stdarg.h header file and expect
  24. it to work with an arbitrary C compiler.
  25.  
  26. A C compiler will provide its own version of one or both of those header
  27. files which, one can only assume, will work correctly with the compiler.
  28. If you stick with use of the compiler-supplied header file and the
  29. usual macros, you should be all right.
  30.  
  31. Unfortunately, the macros in varargs.h and stdarg.h are not compatible,
  32. and many compilers have one header but not the other.  The method
  33. of declaring/defining a variable-arg function also depends on which
  34. set of macros you use, further complicating the issue.
  35.  
  36. If you are lucky, all the compilers you need to use will support
  37. the Standard C <stdarg.h> and allow Standard C function prototypes.
  38. One hopes that more and more compilers, even if remaining backward-
  39. compatible, will support, or at least allow, Standard C features.
  40.  
  41. Otherwise, you will have to use #ifdef's around function declarations,
  42. headers, and macro usage.  This makes the code nearly unreadable.
  43. On the plus side, such functions tend to be rare, so you don't have
  44. to deal with this often.
  45.  
  46. I don't recommend using varargs.h exclusively, since it will make
  47. porting the code to Standard C environments more difficult, and
  48. because varargs.h cannot be used with C++ at all.
  49. -- 
  50.  
  51. Steve Clamage, TauMetric Corp, steve@taumet.com
  52. Vice Chair, ANSI C++ Committee, X3J16
  53.