Page 1 of 3

std::cout or printf

Posted: Sat Oct 27, 2018 6:40 pm
by Uri Blass
I wonder what do you use and if there is an advantage of std::cout

The main advantage that I see for printf is that it is more convenient to use it but when I search I find that people claim that std::cout is more safe

Re: std::cout or printf

Posted: Sat Oct 27, 2018 7:16 pm
by Joost Buijs
Uri Blass wrote: Sat Oct 27, 2018 6:40 pm I wonder what do you use and if there is an advantage of std::cout

The main advantage that I see for printf is that it is more convenient to use it but when I search I find that people claim that std::cout is more safe
I try to avoid the std-library as much as possible, C++ has a lot of nice features but IMHO the std-library is not one of them. When you are using MSVC you can also use printf_s which is also more safe than printf.

Re: std::cout or printf

Posted: Sun Oct 28, 2018 6:45 am
by cdani
I use this for Andscacs, I don't remember why:

Code: Select all

void afout(const char *fmt, ...)
{
	va_list args;
	char buffer[4096];

	va_start(args, fmt);
	vsprintf(buffer, fmt, args);
	va_end(args);

	fprintf(stdout, "%s", buffer);
	fflush(stdout);
}

Re: std::cout or printf

Posted: Sun Oct 28, 2018 7:30 am
by Joost Buijs
In Nightmare I do something similar, basically because I can include the fflush() in just one statement.

Code: Select all

void print(const char *format, ...)
{
	va_list args;
	va_start(args, format);
	vfprintf(stdout, format, args);
	va_end(args);
	fflush(stdout);
}

Re: std::cout or printf

Posted: Sun Oct 28, 2018 9:40 am
by Sven
As always, a good discussion of this topic can be found at Stackoverflow. For chess programs printf() is certainly ok to use. Missing type safety is one of the main counter arguments so we always have to watch out for silly bugs related to format strings.

Re: std::cout or printf

Posted: Sun Oct 28, 2018 2:03 pm
by mar
Sven wrote: Sun Oct 28, 2018 9:40 am Missing type safety is one of the main counter arguments so we always have to watch out for silly bugs related to format strings.
I believe all recent compilers will give you a warning at compile time in those cases.

Re: std::cout or printf

Posted: Sun Oct 28, 2018 2:21 pm
by jdart
Joost Buijs wrote: Sat Oct 27, 2018 7:16 pm
I try to avoid the std-library as much as possible, C++ has a lot of nice features but IMHO the std-library is not one of them
There are a lot of powerful features in the standard library: regex (C++11), streams, iterators, the tools in <algorithms> for example. But there is certainly a learning curve before you are able use those effectively.

Re printf, if you understand C++ <iomanip> you can do all kinds of formatting C++ style and it is not any harder than printf IMO.

--Jon

Re: std::cout or printf

Posted: Sun Oct 28, 2018 3:09 pm
by Rein Halbersma
I’ve been wanting to try https://github.com/fmtlib/fmt
There is an ongoing attempt to have this in the next C++ Standard.

Re: std::cout or printf

Posted: Tue Oct 30, 2018 6:31 pm
by odomobo
Rein Halbersma wrote: Sun Oct 28, 2018 3:09 pm I’ve been wanting to try https://github.com/fmtlib/fmt
There is an ongoing attempt to have this in the next C++ Standard.
I use it in my engine, and it's great, highly recommended.

Re: std::cout or printf

Posted: Wed Oct 31, 2018 3:58 pm
by Ras
I bypass both and use write().