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
std::cout or printf
Moderators: hgm, Rebel, chrisw
-
- Posts: 10311
- Joined: Thu Mar 09, 2006 12:37 am
- Location: Tel-Aviv Israel
-
- Posts: 1564
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: std::cout or printf
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.
-
- Posts: 2204
- Joined: Sat Jan 18, 2014 10:24 am
- Location: Andorra
Re: std::cout or printf
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);
}
Daniel José - http://www.andscacs.com
-
- Posts: 1564
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: std::cout or printf
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);
}
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: std::cout or printf
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)
-
- Posts: 2559
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: std::cout or printf
I believe all recent compilers will give you a warning at compile time in those cases.
Martin Sedlak
-
- Posts: 4367
- Joined: Fri Mar 10, 2006 5:23 am
- Location: http://www.arasanchess.org
Re: std::cout or printf
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.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
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
-
- Posts: 741
- Joined: Tue May 22, 2007 11:13 am
Re: std::cout or printf
I’ve been wanting to try https://github.com/fmtlib/fmt
There is an ongoing attempt to have this in the next C++ Standard.
There is an ongoing attempt to have this in the next C++ Standard.
-
- Posts: 96
- Joined: Fri Jul 06, 2018 1:09 am
- Location: Chicago, IL
- Full name: Josh Odom
Re: std::cout or printf
I use it in my engine, and it's great, highly recommended.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.
-
- Posts: 2488
- Joined: Tue Aug 30, 2016 8:19 pm
- Full name: Rasmus Althoff