//============================================================================
// Name        : MyTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstdarg>


using namespace std;

void PrintAnything(char* format, ...)
{
	char buf[2048];

	va_list args;

	size_t size = 5;
	do
	{
		va_start(args,format);
		cout << "vsnprintf will try size of " << size << endl;
		int n = vsnprintf(buf, size, format, args);
		cout << "n is: " << n << endl;
		if (n == -1 || n >= size)
		{
			size *= 2;
			cout << " double size from to " << size << endl;
		}
		else
		{
			cout << "vsnprintf successful print with size = " << size << " and return n= " << n
					<< " the value=[" << buf << "]" << endl;
			va_end(args);
			break;
		}
		va_end(args);
	}
	while (true);
}


void PrintAnythingWithoutUninitializeVA_List(char* format, ...)
{
	char buf[2048];

	va_list args;
	va_start(args,format);
	size_t size = 5;
	do
	{

		cout << "vsnprintf will try size of " << size << endl;
		int n = vsnprintf(buf, size, format, args);
		cout << "n is: " << n << endl;
		if (n == -1 || n >= size)
		{
			size *= 2;
			cout << " double size from to " << size << endl;
		}
		else
		{
			cout << "vsnprintf successful print with size = " << size << " and return n= " << n
					<< " the value=[" << buf << "]" << endl;

			break;
		}
	}
	while (true);
	va_end(args);
}


int main()
{
	PrintAnything("%s", "a really long string is coming here and we want to see what happens!");

	PrintAnythingWithoutUninitializeVA_List("%s", "a really long string is coming here and we want to see what happens!");


	return 0;
}
