Just how many printf and scanf variants are there? [closed]
There are so many different versions of printf
and scanf
in C that it brings out a chuckle in me. Let's start:
printf
: original implementation; usesformat
then the values as argumentsfprintf
: the same, but takes aFILE
pointer beforeformat
sprintf
: takes achar
pointer beforeformat
snprintf
: same as above, but limits size written for buffer overflow safetyvprintf
: likeprintf
but takes ava_list
of value argumentsvfprintf
: theva_list
equivalent offprintf
vsprintf
: theva_list
equivalent ofsprintf
vsnprintf
: theva_list
equivalent ofsnprintf
asprintf
: takes achar **
beforeformat
and allocates memory on the pointervasprintf
: the same as above, but usesva_list
scanf
: readsformat
into arguments after it fromstdin
fscanf
: takes aFILE
pointer beforeformat
, reading from it insteadsscanf
: takes achar
pointer beforeformat
, reading from it insteadvscanf
: theva_list
function analogical toscanf
vfscanf
: theva_list
function analogical tofscanf
vsscanf
: theva_list
function analogical tosscanf
Thanks to dreamlax, the ones that work with wchar_t
:
wprintf
: original implementation usingwchar_t
everywhere thatchar *
wasfwprintf
: writes to aFILE
pointer beforeformat
, usingwchar_t
swprintf
: writes to achar
pointer beforeformat
, usingwchar_t
vwprintf
: writes tostdin
, taking ava_list
instead of normal argumentsvfwprintf
: writes to aFILE
pointer, taking ava_list
instead of normal argumentsvswprintf
: writes to achar
pointer, taking ava_list
instead of normal arguments
Are there any more?
While there are a lot, usually all but vfprintf
and vfwprintf
are simply wrappers for these two which pass an appropriate FILE *
(possibly a special one setup for writing to a string instead of to a file on disk) and optionally call va_start
and va_end
(depending on if they're the "v" version or the plain version.
You're missing all of the ones that operate on wchar_t
.
精彩评论