LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How does printf work? (https://www.linuxquestions.org/questions/programming-9/how-does-printf-work-270114/)

Ephracis 12-25-2004 12:49 AM

How does printf work?
 
Hi, me again..


I have a class (the language is C++) that has a function used for putting text into a logfile.
Loghandler::log(const char *text);

This works out but now I need a more powerful way to do this. I need to be able to send variables as arguments, like you do to printf().
Loghandler::log(const char *format, ...);

I tried and using ... as type worked, the compiling worked. But how can I manage this? Can I do a function that works like printf and then use it to log the whole, processed string?

itsme86 12-25-2004 03:06 AM

Code:

NAME
      stdarg - variable argument lists

SYNOPSIS
      #include <stdarg.h>

      void va_start( va_list ap, last);
      type va_arg( va_list ap, type);
      void va_end( va_list ap);

DESCRIPTION
      A  function  may  be called with a varying number of argu-
      ments  of  varying  types.  The  include  file  stdarg.h
      declares a type va_list and defines three macros for step-
      ping through a list of arguments whose  number  and  types
      are not known to the called function.

You can find much more information from man stdarg

You're doing right by using ... in the parameter list. the va_*() functions just let you pull those arguments off the stack.


All times are GMT -5. The time now is 04:07 AM.