Using printf in the simulator

Can it be done? It seems to hold all the output until the application is closed.

Edit: when using Windows (not Pokitto) emulation.

Did you end your strings by “\n”?

Unfortunately, yes. One major detail I forgot to add - when simulating in Windows (not Pokitto) setting.

That’s very weird indeed. Does it happen with std::cout ? and LOG ? (which should be preferred to printf by the way)

std::cout works but I guess I was looking to see if I could use printf as then it will work for both Pokitto and Windows emulation. I might wrap these up in a function and detect which environment I am using at compile time.

1 Like

How about LOG? you have to include the Log file first

I am not familiar with LOG - will it work in ‘Pokitto’ mode?

Does std::puts work at all?

it works in both last time I tested it! At first there were inconsistencies with newlines but it was eventually solved

printf under Windows requires a manual call to fflush(stdout) when used in a graphical application.

1 Like

When you tried std::cout, were you using std::endl instead of '\n'?

Thanks all!

The original question was around using printf and it may be solved by using @tuxinator2009’s suggestion of using fflush(stdout) but I haven’t yet tried.

Using std::cout works with both the \n and std::endl.

I will try std::puts (which, like LOG I have never used before).

My preference was for printf simply so I can compile the same code for both Pokitto and Windows.


That’s interesting to know.

The difference is that stream << std::endl acts as if using:


Which would have hinted at flushing potentially being important.

If \n works too then that implies line buffering and flushing is enabled.

One other thing you may want to try is to see if calling std::ios::sync_with_stdio(true) or std::ios::sync_with_stdio(false) makes a difference.
(More info here.)

If std::cout << "something\n" works and std::printf("something\n"); std::fflush(std::stdout); does but std::printf("something\n") doesn’t then the two systems being desynchronised could explain why the C-style IO isn’t working but the C++ IO is.

std::puts is actually the function that prints an unformatted string.
I find it ironic that hardly anyone knows about it.