# C++, printf() and flushing

**URL:** <https://forums.digi.com/t/c-printf-and-flushing/5575>\
**Category:** NET+OS\
**Created:** [May 29, 2007, 1:13pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575 "2007-05-29T13:13:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![compie](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@compie](https://forums.digi.com/u/compie)\
**Post date:** [May 29, 2007, 1:13pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/1 "2007-05-29T13:13:23Z")

</div>

I’m using the C++ sample from NET+OS 7.1.

When I use cout it works correctly during debugging (if I step over the statement the whole line is printed on the serial console).

When I use printf it doesn’t print the whole line on the serial console. How can I make printf automatically flush its output?

From the root.cxx:

#ifndef NETOS\_GNU\_TOOLS  
printf(…);  
#else  
cout \<\< …;  
#endif

So the sample uses cout by default, but I need printf.

---

<div class="post-metadata">

**Author:** ![DanielS](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DanielS](https://forums.digi.com/u/DanielS)\
**Post date:** [May 29, 2007, 1:37pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/2 "2007-05-29T13:37:06Z")

</div>

this is odd. because cout shouldn’t flush it’s cache also. for flushing you can append “\<\< endl;” for cout and "  
" for printf

---

<div class="post-metadata">

**Author:** ![charliek](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@charliek](https://forums.digi.com/u/charliek)\
**Post date:** [May 29, 2007, 3:13pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/3 "2007-05-29T15:13:15Z")

</div>

You should be able to call  
as Daniel states to see the whole output. Also, if you’re calling printf from multiple threads you’ll need to mutex protect the calls to printf, otherwise they’ll stomp on eachother.

---

<div class="post-metadata">

**Author:** ![compie](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@compie](https://forums.digi.com/u/compie)\
**Post date:** [May 30, 2007, 7:42am UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/4 "2007-05-30T07:42:17Z")

</div>

In the sample code both printf and cout are using a end of line at the end. (  
and endl). So both should flush.

And how do you know that printf needs mutexes if I call it from multiple threads? Can I find that in the documentation somewhere?

---

<div class="post-metadata">

**Author:** ![compie](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@compie](https://forums.digi.com/u/compie)\
**Post date:** [May 30, 2007, 2:21pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/5 "2007-05-30T14:21:34Z")

</div>

I’m still having problems with flushing. I have a breakpoint on the first line in applicationStart() and this is what I get on the serial port:

IAM:Got AUTO-CONFIGURED IPv6 address FE80::240:9DFF:FE43:3597 on interface eth0:3  
IAM:Got DHCP IPv4 address 1

I first have to printf() something myself before it continues with  
“30.144.45.14 on interface eth0  
Network IP configured.”

---

<div class="post-metadata">

**Author:** ![charliek](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@charliek](https://forums.digi.com/u/charliek)\
**Post date:** [May 30, 2007, 3:35pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/6 "2007-05-30T15:35:59Z")

</div>

I’m not aware of any place in the documentation that specifies this (and a quick search of the API reference confirms this). I know this, however, from speaking with Digi’s Engineering team about a case a few months back.

---

<div class="post-metadata">

**Author:** ![charliek](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@charliek](https://forums.digi.com/u/charliek)\
**Post date:** [May 30, 2007, 3:40pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/7 "2007-05-30T15:40:41Z")

</div>

Does this happen with one of the template apps or just your own custom app? If only on yours, what changes have you made?

---

<div class="post-metadata">

**Author:** ![compie](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@compie](https://forums.digi.com/u/compie)\
**Post date:** [May 31, 2007, 3:04pm UTC](https://forums.digi.com/t/c-printf-and-flushing/5575/8 "2007-05-31T15:04:11Z")

</div>

It happens with the C++ sample.  
The breakpoint is on the line

```
 app_down.Name(app_name);

```
