The difference between the printf and vprintf is , vprintf() function takes an extra argument which points to a list of arguments.

  • vprintf() is used to write custom printf() like function.

practical example of using vprintf

Write your own logging utility which prints file , function , line number with time stamp.

Compile and run

How it works

  • The function Log(char *format , … ) , … indicates any number of arguments.
  • va_list points to the arguments list.
  • va_start(args, format) initializes args variable to be used with the va_arg and va_end macros.
  • vfprintf (stdout, format, args); prints each argument to the screen.
  • The va_arg, va_end, and va_start macros provide a portable way to access the arguments to a function when the function takes a variable number of arguments.

Ref:

http://www.cplusplus.com/reference/cstdio/vprintf/




Related Contents to follow