C Tutorial/stdio.h/vprintf vfprintf vsprintf vsnprintf

Материал из C\C++ эксперт
Перейти к: навигация, поиск

vprintf, vfprintf, vsprintf, and vsnprintf

Item Value Header file stdarg.h stdio.h Declaration int vprintf(char *format, va_list arg_ptr); int vfprintf(FILE *stream, const char *format, va_list arg_ptr); int vsprintf(char *buf, const char *format, va_list arg_ptr); int vsnprintf(char * restrict buf, size_t num, const char * restrict format, va_list arg_ptr);

The functions vprintf(), vfprintf(), vsprintf(), and vsnprintf() are functionally equivalent to printf(), fprintf(), sprintf(), and snprintf(), respectively , except that the argument list has been replaced by a pointer to a list of arguments.

This pointer must be of type va_list, which is defined in the header <stdarg.h>.


<source lang="cpp">#include <stdio.h>

 #include <stdarg.h>
 void print_message(char *format, ...)
 {
   va_list ptr;
   va_start(ptr, format);
   vprintf(format, ptr);
   va_end(ptr);
 }
 int main(void)
 {
   print_message("Cannot open file %s.", "test");
   return 0;
 }</source>