vsnprintf, _vsnprintf, vsnwprintf, _vsnwprintf

From RAD Studio
Jump to: navigation, search

Go Up to stdio.h Index


Header File

stdio.h

Category

Memory and String Manipulation Routines

Prototype

int _vsnprintf(char* buffer, size_t nsize, const char* format, va_list param);
int _vsnwprintf(wchar_t* buffer, size_t nsize, const wchar_t* format, va_list param);

Description

Sends formatted output to a buffer of maximum length specified by nsize.

If the number of bytes to output is:

  • < nsize, then all of the characters have been written, including the terminating '\0' character.
  • == nsize, then nsize characters are written, with no terminating '\0' character.
  • > nsize, then only nsize characters are written, with no terminating '\0' character.

If nsize is 0, then the string will not be written to (and may be NULL).

Return Value

The number of bytes output, or, if nsize is 0, the number of bytes needed, not including the terminating '\0' character.

Portability

POSIX Win32 ANSI C ANSI C++

+

+

+


Example

#include <stdio.h>
#include <wtypes.h>

void Vprint_test(char *format, ...)
{

   int n_byte = 0;
   char buff[30];
   buff[0]=0;

   va_list param;  //the list of parameters
   va_start(param, format);
   n_byte = vsnprintf( buff, sizeof(buff) - 1, format, param);

   //Compare the buffer's length with the byte written to see whether the buffer was big enough
	if (n_byte<sizeof(buff)) {
		  printf("all characters were written:(%d bytes)\n buff: %s\n", n_byte, buff);
	} else
	{
		if (n_byte==sizeof(buff)) {
			printf("%d characters were written(with no terminating character):(%d bytes)\n buff: %s\n", sizeof(buff),n_byte, buff);

		}else
			printf("only %d characters were written(with no terminating character):(%d bytes)\n buff: %s\n", sizeof(buff),n_byte, buff);

	}
}

int main() {
   Vprint_test("%s %d %f", "Hello", 123, 23.45);
	 return 0;
}

See Also