home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11591 < prev    next >
Encoding:
Text File  |  1992-07-25  |  2.3 KB  |  82 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!gate.fzi.de!dockhorn
  3. From: dockhorn@fzi.de (Patrick Dockhorn)
  4. Subject: Summary: Macro with variable number of arguments
  5. Message-ID: <1992Jul25.140204.17746@fzi.de>
  6. Followup-To: poster
  7. Keywords: macro arguments variable number summary
  8. Sender: news@fzi.de (FZI-news)
  9. Reply-To: dockhorn@fzi.de
  10. Organization: Forschungszentrum Informatik (FZI), Karlsruhe, Germany
  11. Date: Sat, 25 Jul 1992 14:02:04 GMT
  12. Lines: 68
  13.  
  14. Two weeks ago I posted a request to solve the problem 
  15. of having a macro with a variable number of arguments.
  16. In fact, the question was a little bit more detailed:
  17. I wanted a macro that allowed me to pass parameters to it
  18. in the way you call the printf - function.
  19. I knew of the solution with the double parenthesis, i.e.
  20.  
  21. #define DEBUG_OUT (x) printf x
  22.  
  23. and calling the macro with e.g.
  24.  
  25. DEBUG_OUT(("File %s, Line %d : Fatal error !",__FILE__,__LINE__));
  26.  
  27.  
  28. My special problem was how to use this trick to write the error text to
  29. stderr instead of stdout. The straight-forward solution to this was given
  30. to me by Leo (leo@ine.philips.nl) and Jos (jos@and.nl):
  31.  
  32. #define DEBUG_OUT(x) fprintf x
  33.  
  34. and then use it (again) with the argument list enclosed in double parentheses:
  35.  
  36. DEBUG_OUT((stderr,
  37.        "file %s, line %d - error %d occured.",
  38.            __FILE__,__LINE__,error_code));
  39.  
  40.  
  41. The other solutions all mentioned the use of a variable arg list function;
  42. they all looked more or less like this:
  43.  
  44. #define DEBUG_OUT(x) eprintf x
  45.  
  46. #include <stdarg.h>    /* vararg.h on some systems */
  47. #include <stdio.h>
  48.  
  49. /* just like printf, but to stderr */
  50. int eprintf(const char *format, ...) 
  51. {
  52.  va_list args;
  53.  int retval;
  54.  
  55.  va_start(args, format);
  56.  retval = vfprintf(stderr, format, args);
  57.  va_end(args);
  58.  return retval;
  59. }
  60.  
  61.  
  62. I'd like to thank the following net.people for their
  63. suggestions; they have been very helpful:
  64.  
  65. Fergus Henderson     (fjh@munta.cs.mu.OZ.AU),
  66. Gene Ressler         (ressler@cs.cornell.edu),
  67. Michael Gordon        (mfg@castle.edinburgh.ac.uk),
  68. Patrick Schaaf         (bof@midget.saar.de)
  69. Raymond Chen        (raymondc@microsoft.com)
  70. Jutta Degener         (jutta@cs.tu-berlin.de)
  71.  
  72.  
  73.     Patsch
  74.  
  75. -- 
  76. Patrick Dockhorn           Forschungszentrum Informatik, Karlsruhe/Germany.
  77. INTERNET: dockhorn@fzi.de      There are two rules for success in life:
  78. BITNET  : ukew@dkauni2.bitnet  Rule 1:  Don't tell people everything you know.
  79.  
  80.  
  81.  
  82.