2

This is kind of a weird behavior. Basically, I have this code:

stdout.printf("Enter some random number: ");
int number = int.parse(stdin.read_line());
stdout.printf(number.to_string());
stderr.printf("THIS IS AN ERROR!\n");

Now what's weird to me is that when I compile and run this code, I get this output:

Enter some random number: 2
THIS IS AN ERROR!
2user@host:...

But if I add a line that says stdout.printf("\n"); between the last two commands, I get this as in input:

Enter some random number: 2
2
THIS IS AN ERROR!
user@host

Why is this happening? Why is Vala outputting the number after the sterr.printf() output in the first example and before in the second one? Shouldn't it just print 2THIS IS AN ERROR! in the first example?

r3bl
  • 260
  • 5
  • 15

1 Answers1

3

Vala is just using C's fprintf underneath, which displays these semantics. Have a look at Why does printf not flush after the call unless a newline is in the format string?

You can call stdout.flush() if you want.

Community
  • 1
  • 1
apmasell
  • 7,033
  • 19
  • 28
  • Also there are deeper mechanisms of how the terminal works that surface here. See https://en.wikipedia.org/wiki/Computer_terminal#Modes for a discussion of "line-at-a-time mode" and how this is done in POSIX.- – Jens Mühlenhoff Jul 29 '15 at 00:59