Scroll Down to See All
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfclosefdopenfeofferrorfflushfgetcfgetposfgetsfgetwcfgetwsfopenfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwritefwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmallocmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetcungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1yn



Function Details : vsnprintf

intvsnprintf(char * str,size_t size,const char * format,va_list args) ;

Return Type : int

Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
Read about return values of vsnprintf function .

1st Parameter Type : char *

String pointer (array of characters)

1st Parameter

Pointer to the buffer where the formatted output string will be stored. The buffer must be large enough to hold the resulting string, including the null terminator, or `NULL` if only the length is needed.

2nd Parameter Type : size_t

Platform-specific unsigned type for array indices and memory sizes.

2nd Parameter

The maximum number of characters to write to the buffer, including the null terminator. If `size` is 0, nothing is written, but the function still computes the length of the formatted string.

3rd Parameter Type : const char *

Pointer to read-only string

3rd Parameter

Pointer to a null-terminated string specifying the format of the output. It may include format specifiers like `%d`, `%s`, etc.

4th Parameter Type : va_list

A data type used to hold information about variable arguments passed to a variadic function. It is initialized with `va_start`, used with `va_arg`, and cleaned up with `va_end`.

4th Parameter

The variable argument list containing the values to be formatted and written. It must be initialized with `va_start`.

Read more about parameters of vsnprintf in parameters section
The vsnprintffunction in C language Writes formatted output to a string with size limitation using a va_list.
vsnprintf is similar to snprintf, but instead of taking a variable number of arguments, it takes a va_list that has been initialized by va_start. This function writes at most size - 1 characters to the output string, ensuring it's always null-terminated. It's a safer alternative to vsprintf as it prevents buffer overflows.
The vsnprintffunction takes 4 parameters:
  • char * `str`: Pointer to the buffer where the formatted output string will be stored. The buffer must be large enough to hold the resulting string, including the null terminator, or `NULL` if only the length is needed.
  • size_t `size`: The maximum number of characters to write to the buffer, including the null terminator. If `size` is 0, nothing is written, but the function still computes the length of the formatted string.
  • const char * `format`: Pointer to a null-terminated string specifying the format of the output. It may include format specifiers like `%d`, `%s`, etc.
  • va_list `args`: The variable argument list containing the values to be formatted and written. It must be initialized with `va_start`.
Writes a formatted string to the buffer `str` with a limit of `size` characters, including the null terminator. If the formatted string exceeds the buffer size, it is truncated. Returns the number of characters that would have been written if the buffer were large enough (excluding the null terminator), or a negative value if an encoding error occurs.
The vsnprintf function return value :
  • Returns the number of characters that would have been written if size had been sufficiently large, not counting the terminating null character
  • If an encoding error occurs, a negative number is returned

Output

This example demonstrates using vsnprintf in a custom function to safely format a string with various types of arguments, even when the buffer is too small to hold the entire formatted string.