topical media & game development

talk show tell print

professional-program-12-VarArgs-VarArgs.c

? / professional-program-12-VarArgs-VarArgs.c


  include <cstdio>
  include <cstdarg>
  include <iostream>
  using namespace std;
  
  bool debug = false;
  
  void debugOut(char* str, ...)
  {
    va_list ap;
    if (debug) {
      va_start(ap, str);
      vfprintf(stderr, str, ap);
      va_end(ap);
    }
  }
  
  void printInts(int num, ...)
  {
    int temp;
    va_list ap;
    va_start(ap, num);
    for (int i = 0; i < num; i++) {
      temp = va_arg(ap, int);
      cout << temp << " ";
    }
    va_end(ap);
    cout << endl;
  }
  
  int main(int argc, char** argv)
  {
    debug = true;
    debugOut("int \%d\n", 5);
    debugOut("String \%s and int \%d\n", "hello", 5);
    debugOut("Many ints: \%d, \%d, \%d, \%d, \%d\n", 1, 2, 3, 4, 5);
  
    printInts(5, 5, 4, 3, 2, 1);
  
    return (0);
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.