Title: what's the difference between the printf and vprintf function families, and when should I use one over the other?
Tags: c
Question: I understand that the difference between the printf, fprintf, sprintf etc functions and the vprintf, vfprintf, vsprintf etc functions has to do with how they deal with the function arguments. But how specifically? Is there really any reason to use one over the other? Should I just always use printf as that is a more common thing to see in C, or is there a legitimate reason to pick vprintf instead?
Comment: I recommend adding the tags "printf" and "variadic-functions" to your question.

Here is the accepted answer: printf() and friends are for normal use. vprintf() and friends are for when you want to write your own printf()-like function. Say you want to write a function to print errors:
int error(char *fmt, ...)
int result;
va_list args;
va_start(args, fmt);
// what here?
va_end(args);
return result;
You'll notice that you can't pass args to printf(), since printf() takes many arguments, rather than one va_list argument. The vprintf() functions, however, do take a va_list argument instead of a variable number of arguments, so here is the completed version:
int error(char *fmt, ...)
int result;
va_list args;
va_start(args, fmt);
fputs("Error: ", stderr);
result = vfprintf(stderr, fmt, args);
va_end(args);
return result;
Comment for this answer: Note: the 'v' stands for "variadic". Knowing that is useful to help me remember what the `vprintf` functions even are, since reminding myself what the 'v' stands for also reminds me what these functions are for. More on "variadic" here: https://en.wikipedia.org/wiki/Variadic_function.
Comment for this answer: time to learn about variadic functions! This answer cleared things up, thank you.
Comment for this answer: Of course, then you want a `warn()` function that works like `error()` but prints "Warning: " instead, so you create your _own_ `vprintf()` style function: `int verror(char *prefix, char *fmt, va_list args);` and have `error()` call that with "Error: " and `warn()` call it with "Warning: " but it's all semantics.

Here is another answer: This example shows the elegance of Python. Since C/C++ cannot reconcile the difference between int error(char *fmt, ...) and int error(char *fmt, va_list ap), thus, for every function *printf, it has to create two versions, i.e., one taking in ..., the other taking in va_list, this essentially doubles the total number of functions. In Python, you can use *list() or **dict() to pass in a va_list as ....
Hopefully, future C/C++ can support this kind of argument processing scheme.

Here is another answer: The main difficulty with variadic arguments is not that there is a variable number of arguments but that there is no name associated with each argument. The va_start, va_arg macros parse the arguments in memory (in most C compilers they are on the stack) using the type information contained in the format string cf. Kernighan and Ritchie, second edition, section 7.3.

Here is another answer: You never want to use vprintf() directly, but it's incredibly handy when you need to e.g. wrap printf(). For these cases, you will define the top-level function with variable arguments (...). Then you'll collect those into a va_list, do your processing, and finally call vprintf() on the va_list to get the printout happening.
Comment for this answer: Never say never.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:09/28/09', 'FavoriteCount': 27.0, 'PostTypeId': 1, 'Tags': '<c>', 'Title': "what's the difference between the printf and vprintf function families, and when should I use one over the other?", 'ViewCount': 26493, 'id': 1485805, 'user_AboutMe': '', 'user_AccountId': 30883.0, 'user_DisplayName': 'Carson Myers', 'user_Reputation': 36370.0, 'user_Views': 3451.0, 'user_id': 84478.0}}