I believe the prefixed literals are a direct response to this style of multiline string. The style you showed is pretty common in a programming languages, but language implementers are faces with a tradeoff:
- Keep the initial indentation for each line in the string literal; or
- Track the indentation level and attempt to remove the whitespace for each line.
For a lot of strings, extra whitespace doesn't matter (eg. SQL), but when you don't want it, you end up removing the indentation in the string literal, and having a string like this:
fn f() {
text = "This is a decent way to format strings,
but surely it could be a little nicer indentation-wise,
right?";
print(text);
}
The prefixed lines have the disadvantage of being a pain to use if your editor doesn't have nice multi-line editing like Vim or Sublime. But I think it's a nice option when it's available.
No, printf does not flush when \n is encountered, unless you specify it with setvbuf(stdout, NULL, _IOLBF, 0);
And data files don't generally end with a newline.
POSIX buffers aren't generally flushed until fflush or fclose is called (or when the buffer is full). fclose is automatically called on exit(), which is why explicit flushing isn't usually needed. Note that this requires stdio to maintain a global list of open files, which is something that the Zig stdlib doesn't do for numerous good reasons.
reply