<p>In C++, there are two ways of printing something: <code>printf()</code> or <code>cout &lt;&lt;</code>.</p>
<p>However, in Python, there's only one way: <code>print()</code>.</p>
<p>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?</p>

<functions><print>
What are the pros/cons of having multiple ways to print?
CC BY-SA 4.0
2023-06-28T07:09:06.183Z
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')`.
Michael Homer
CC BY-SA 4.0
2023-06-28T07:56:06.327Z
@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
kouta-kun
CC BY-SA 4.0
2023-06-28T08:00:51.453Z
@kouta-kun Well, a file stream is exactly what `cout` is too, but I guess that's arguable.
Michael Homer
CC BY-SA 4.0
2023-06-28T08:06:09.060Z
@MichaelHomer that is true, but C++ is rare among programming languages in that it conflates file/stream output with formatting operations.
kouta-kun
CC BY-SA 4.0
2023-06-30T16:57:05.220Z
Note that `printf` is mostly just a C holdover. It's not really used in C++ proper and not usually recommended.
Chipster
<p>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 <code>stdout</code> for the lowest level, and <code>printf</code> and <code>cout</code> on top of it. Python provides <code>sys.stdout</code> as a stream object, as well as <code>print</code> and the <code>logging</code> module on top. Java has <code>System.out</code> as a stream object, and some wrappers like <code>java.util.Logger</code> around it.</p>
<p>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 <code>fmt</code> for C++ (until it was added to C++20's standard library under <code>std::format</code>). 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.</p>

CC BY-SA 4.0
2023-06-28T09:23:02.583Z
2023-06-28T09:23:02.583Z
abel1502
<p>C++ specifically inherits <code>printf</code> from C, so compatibility is a big reason.</p>
<p>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 &quot;print a specific object&quot; 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.</p>

CC BY-SA 4.0
2023-06-28T07:09:03.100Z
2023-06-28T07:09:03.100Z
kouta-kun
<p>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.</p>

CC BY-SA 4.0
2023-06-28T15:22:28.843Z
2023-06-28T15:22:28.843Z
Nuoji
<h2>Pro</h2>
<p>Each of the different ways may be best suited for certain scenarios. Consider in C <code>printf(&quot;Hello&quot;)</code> vs <code>fprintf(stdout, &quot;Hello&quot;)</code>. They do the same and when printing to the console you may prefer just <code>printf</code>, but <code>fprintf</code> gives you more flexibility in letting you specify the target <code>FILE</code>.</p>
<h2>Cons</h2>
<ul>
<li>For beginners it's more overhead when learning the language. When should I use each method? How are they different?</li>
<li>More code for the implementers to maintain.</li>
<li>Need to make sure that different implementations behave in a way that makes sense across all of them. e.g.: I expect <code>fprintf(stdout, &quot;Hello&quot;)</code> and <code>printf(&quot;Hello&quot;)</code> to do the same.</li>
</ul>

CC BY-SA 4.0
2023-06-28T19:51:46.323Z
2023-06-28T19:51:46.323Z
Alexander Goussas
<blockquote>
<p>What are the pros and cons of having more than one feasible printing function?</p>
</blockquote>
<p>Here are a few possible cons:</p>
<ul>
<li>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.</li>
<li>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, <code>print</code> adds a linebreak by default, but <code>write</code> doesn't, so it's easy to forget linebreaks or add redundant linebreaks, if you mix the two functions.</li>
</ul>
<p>I won't discuss the pros, because unless you <em>want</em> a <a href="https://esolangs.org/wiki/Turing_tarpit" rel="noreferrer">minimalistic language</a>, you're inevitably going to end up with more than one way to do anything.</p>
<p>Even just using <code>print</code> in python, here are five ways to print the same thing:</p>
<pre><code>print('Hello', username)
print('Hello {}'.format(username))
print(f'Hello {username}')
print(' '.join(('Hello', username)))
print('Hello', end=' '); print(username)
</code></pre>

CC BY-SA 4.0
2023-06-28T12:01:16.763Z
2023-06-28T12:01:16.763Z
Stef
# What are the pros/cons of having multiple ways to print?
Posted by: username_0 on 2023-06-28
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?
**Tags:** functions print
> **Comments:**
> **username_1**: 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')`.  
> **username_2**: @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  
> **username_1**: @kouta-kun Well, a file stream is exactly what `cout` is too, but I guess that's arguable.  
> **username_2**: @MichaelHomer that is true, but C++ is rare among programming languages in that it conflates file/stream output with formatting operations.  
> **username_3**: Note that `printf` is mostly just a C holdover. It's not really used in C++ proper and not usually recommended.  
---
## 5 answers
### Answer by username_7 on 2023-06-28. Score: 12 | Accepted answer
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)
---
### Answer by username_4 on 2023-06-28. Score: 6
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.
---
### Answer by username_2 on 2023-06-28. Score: 5
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.
---
### Answer by username_5 on 2023-06-28. Score: 3
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.
---
### Answer by username_6 on 2023-06-28. Score: 2
## 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.
---
