7
$\begingroup$

In C++, there are two ways of printing something: printf() or cout <<.

However, in Python, there's only one way: print().

I'm not aware of any other languages that have multiple ways to print. What are the pros and cons of having more than one feasible printing function?

$\endgroup$
5
  • 22
    $\begingroup$ I'm not sure the premise here is true. It wouldn't be surprising if most real-world languages had multiple ways to print; even in Python you can say sys.stdout.write('hello, world\n'). $\endgroup$
    – Michael Homer
    Jun 28 at 7:09
  • 1
    $\begingroup$ @MichaelHomer to that end, is a file stream really a method of printing? It can be used as such sure but filestream.write provides practically no facilities for logging and just happens to work that way because Unix made terminals files, while printing functions involve a whole bunch of facilities on formatting $\endgroup$
    – kouta-kun
    Jun 28 at 7:56
  • 6
    $\begingroup$ @kouta-kun Well, a file stream is exactly what cout is too, but I guess that's arguable. $\endgroup$
    – Michael Homer
    Jun 28 at 8:00
  • 1
    $\begingroup$ @MichaelHomer that is true, but C++ is rare among programming languages in that it conflates file/stream output with formatting operations. $\endgroup$
    – kouta-kun
    Jun 28 at 8:06
  • 2
    $\begingroup$ Note that printf is mostly just a C holdover. It's not really used in C++ proper and not usually recommended. $\endgroup$
    – Chipster
    Jun 30 at 16:57

5 Answers 5

12
$\begingroup$

What are the pros and cons of having more than one feasible printing function?

Here are a few possible cons:

  • Different printing functions might use different buffers, messing up the order that things are printed in if the buffers are not flushed before switching to a different printing function.
  • Different printing functions might be extremely similar, yet follow slightly different conventions, leading to inconsistent behaviour and mistakes by the programmer; for instance in python, print adds a linebreak by default, but write doesn't, so it's easy to forget linebreaks or add redundant linebreaks, if you mix the two functions.

I won't discuss the pros, because unless you want a minimalistic language, you're inevitably going to end up with more than one way to do anything.

Even just using print in python, here are five ways to print the same thing:

print('Hello', username)
print('Hello {}'.format(username))
print(f'Hello {username}')
print(' '.join(('Hello', username)))
print('Hello', end=' '); print(username)
$\endgroup$
0
6
$\begingroup$

Most languages I know of provide one way to directly intreact with the standard output stream, as well as some higher-level formatted output capabilities. C++ has C's file API and stdout for the lowest level, and printf and cout on top of it. Python provides sys.stdout as a stream object, as well as print and the logging module on top. Java has System.out as a stream object, and some wrappers like java.util.Logger around it.

Overall, it seems reasonable to provide a single uniform way to interact with the output as a text stream, and then let more specialized API be built on top of it. Some common cases might be implemented in the standard library - like basic formatted printing, for instance. The less common ones could be implemented in community modules - like fmt for C++ (until it was added to C++20's standard library under std::format). The benefit to this approach is that the program's output isn't something special, but rather just a specific text stream, and the corresponding API would work just as well with writing to a file, a socket or a buffer.

$\endgroup$
5
$\begingroup$

C++ specifically inherits printf from C, so compatibility is a big reason.

Java actually has multiple, native ways to print, namely system.out.print/println vs system.out.printf. In that case, the advantage is that one is good old, quick and dirty "print a specific object" vs printing with a format specifier. The downside is that you split your users into people who misleadingly just go for println and implement their own formatting and people who actually use printf.

$\endgroup$
3
$\begingroup$

This is not about the language, but about the standard library. The standard library itself will be affected by the features of the language. C++, inheriting both the stdlib and features from C, opted to add another print that leveraged the features on top of C.

$\endgroup$
2
$\begingroup$

Pro

Each of the different ways may be best suited for certain scenarios. Consider in C printf("Hello") vs fprintf(stdout, "Hello"). They do the same and when printing to the console you may prefer just printf, but fprintf gives you more flexibility in letting you specify the target FILE.

Cons

  • For beginners it's more overhead when learning the language. When should I use each method? How are they different?
  • More code for the implementers to maintain.
  • Need to make sure that different implementations behave in a way that makes sense across all of them. e.g.: I expect fprintf(stdout, "Hello") and printf("Hello") to do the same.
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .