std::cout or printf

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Uri Blass
Posts: 8558
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

std::cout or printf

Post by Uri Blass » Sat Oct 27, 2018 4: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

Joost Buijs
Posts: 960
Joined: Thu Jul 16, 2009 8:47 am
Location: Almere, The Netherlands

Re: std::cout or printf

Post by Joost Buijs » Sat Oct 27, 2018 5:16 pm

Uri Blass wrote:
Sat Oct 27, 2018 4: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: 2104
Joined: Sat Jan 18, 2014 9:24 am
Location: Andorra
Contact:

Re: std::cout or printf

Post by cdani » Sun Oct 28, 2018 5:45 am

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: 960
Joined: Thu Jul 16, 2009 8:47 am
Location: Almere, The Netherlands

Re: std::cout or printf

Post by Joost Buijs » Sun Oct 28, 2018 6:30 am

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: 3822
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: std::cout or printf

Post by Sven » Sun Oct 28, 2018 8:40 am

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: 1992
Joined: Fri Nov 26, 2010 1:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: std::cout or printf

Post by mar » Sun Oct 28, 2018 1:03 pm

Sven wrote:
Sun Oct 28, 2018 8: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: 3817
Joined: Fri Mar 10, 2006 4:23 am
Location: http://www.arasanchess.org

Re: std::cout or printf

Post by jdart » Sun Oct 28, 2018 1:21 pm

Joost Buijs wrote:
Sat Oct 27, 2018 5: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: 685
Joined: Tue May 22, 2007 9:13 am

Re: std::cout or printf

Post by Rein Halbersma » Sun Oct 28, 2018 2: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.

odomobo
Posts: 59
Joined: Thu Jul 05, 2018 11:09 pm
Location: Chicago, IL
Full name: Josh Odom

Re: std::cout or printf

Post by odomobo » Tue Oct 30, 2018 5:31 pm

Rein Halbersma wrote:
Sun Oct 28, 2018 2: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: 1143
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: std::cout or printf

Post by Ras » Wed Oct 31, 2018 2:58 pm

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

Post Reply