std::cout or printf

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Uri Blass
Posts: 10268
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

std::cout or printf

Post 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
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: std::cout or printf

Post 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.
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: std::cout or printf

Post 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);
}
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: std::cout or printf

Post 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);
}
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: std::cout or printf

Post 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.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: std::cout or printf

Post 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.
Martin Sedlak
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: std::cout or printf

Post 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
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: std::cout or printf

Post 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.
odomobo
Posts: 96
Joined: Fri Jul 06, 2018 1:09 am
Location: Chicago, IL
Full name: Josh Odom

Re: std::cout or printf

Post 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.
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: std::cout or printf

Post by Ras »

I bypass both and use write().
Rasmus Althoff
https://www.ct800.net