3

This code:

#include <iostream>

int main() {
    std::cout << "---" << std::endl;
    std::cout << char() << std::endl;
    std::cout << "---" << std::endl;
}

prints this:

---
---

Why isn't std::endl causing a new line to be between the other two?

Removing char() << makes the output become what I expect:

---

---
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Enlico
  • 23,259
  • 6
  • 48
  • 102
  • If you try e.g. `std::cout << "||" << char() << "||" << std::endl;` what is printed then? Do you get all four bars? – Some programmer dude Aug 12 '23 at 17:28
  • By the way, what terminal are you using? Perhaps it has some special handling for the null character? – Some programmer dude Aug 12 '23 at 17:29
  • 5
    That's going to be an interpretation by your terminal. There is a null character in the output, which I think a terminal should just ignore as non-printable character, but I doubt that this is standardized anywhere. What terminal are you looking at? – user17732522 Aug 12 '23 at 17:29
  • 1
    It's reproducible on godbolt: https://godbolt.org/z/nfEcEPx6z – jabaa Aug 12 '23 at 17:30
  • I've added the link to compiler explorer. – Enlico Aug 12 '23 at 17:35
  • 1
    @jabaa GCC still produces the null character and newline character on stdout though. It gets skipped by the presentation on compiler explorer. That can easily happen, even unintentionally, if for example if something in the rendering code is handling each line individually, accidentally as a null-terminated string. – user17732522 Aug 12 '23 at 17:36
  • 1
    Strange. I've tried it with `sh`, `bash` and `zsh`. All print the newline. I can only reproduce it on Compiler Explorer. What's your actual environment? – jabaa Aug 12 '23 at 17:38
  • 2
    It seems like this is not a question about C++, but about how Compiler Explorer prints the output. It's open source: https://github.com/compiler-explorer/compiler-explorer – jabaa Aug 12 '23 at 17:46
  • 1
    Godbolt doctors its output a bit to hide some of its internals. For example: https://godbolt.org/z/TE84j7so9 – Jerry Coffin Aug 12 '23 at 17:46
  • Yeah, I'd file an issue on the Github repo, I don't think Stack Overflow is the place for bug reports. – Etienne de Martel Aug 12 '23 at 18:33
  • 1
    It'd be nice if godbolt would output `␀` for a `char()`. Next time I see Matt Godbolt, I'll mention it to him. – Eljay Aug 13 '23 at 11:32

1 Answers1

4

The code will cause the standard library to print a null character followed by a newline character to the stream.

How these characters are presented to you is a matter of the terminal or other UI you are using to view the stream's contents.

I would normally expect a terminal to skip the null character as non-printable and to interpret the newline as usual. However, I don't think there is any kind of standard or specification requiring this behavior.

In this concrete case I suspect a bug in how compiler explorer presents the output, probably in AnsiToHtml that is based on https://github.com/rburns/ansi-to-html and used to format output of the compiler containing ANSI escape codes. The library does some tokenization of the input, so a bug of this kind doesn't seem unlikely, but I don't know enough JavaScript/TypeScript to figure this out with a quick look.

user17732522
  • 53,019
  • 2
  • 56
  • 105