├── Examples.md └── README.md /Examples.md: -------------------------------------------------------------------------------- 1 | # Fixed Hello World examples 2 | 3 | As mentioned in the [README.md](README.md), many languages do have a way 4 | to explicitly test for errors on stdout. This file collects examples of 5 | how to do this in various languages. 6 | 7 | ## C 8 | 9 | ```c 10 | #include 11 | #include 12 | 13 | int main(void) { 14 | printf("Hello, World!\n"); 15 | 16 | if (fflush(stdout) != 0 || ferror(stdout) != 0) { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | return EXIT_SUCCESS; 21 | } 22 | ``` 23 | 24 | ## C++ 25 | 26 | ```c++ 27 | #include 28 | #include 29 | 30 | int main(int, char**) 31 | { 32 | std::cout << "Hello World!\n" << std::flush; 33 | 34 | return (std::cout.rdstate()) ? EXIT_FAILURE : EXIT_SUCCESS; 35 | } 36 | ``` 37 | 38 | ## Lua 39 | 40 | ```lua 41 | io.write("Hello World\n") 42 | assert(io:flush()) 43 | ``` 44 | 45 | ## Go 46 | 47 | ```go 48 | package main 49 | 50 | import "fmt" 51 | 52 | func main() { 53 | if _, err := fmt.Println("Hello, World!"); err != nil { 54 | panic(err) 55 | } 56 | } 57 | ``` 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hello World vs. I/O errors 2 | 3 | In some programming languages, the "Hello, World!" tutorial programs report I/O 4 | errors in their exit status. In others, they silently swallow I/O errors, which 5 | doesn't satisfy common expectations of command-line programs. 6 | 7 | Hello world programs are just toy examples. And, most languages do have ways to 8 | detect I/O errors on standard output. So, this page is effectively about 9 | defaults and ergonomics, in a specific context. 10 | 11 | See also [the original blog post] for more discussion. 12 | 13 | ## Languages with the bug 14 | 15 | | Language | Notes | How to fix the bug 16 | | ---------- | ------------------------------------------ | ----------------------------- 17 | | C | The C standard specifies this behavior | [Example](Examples.md#C) 18 | | C++ | The C++ standard specifies this behavior | [Example](Examples.md#C++) 19 | | Crystal | `Crystal 1.3.2 (2022-01-19)` | 20 | | Erlang/OTP | [Before Erlang/OTP 26.0](https://github.com/erlang/otp/issues/5797) 21 | | Fortran | `GNU Fortran 11.2.0` | 22 | | Go | https://news.ycombinator.com/item?id=30612821 | [Example](Examples.md#Go) 23 | | Haxe/HashLink| `Haxe 4.2.4 + HashLink 1.11.0` | 24 | | Haskell | `The Glorious Glasgow Haskell Compilation System, version 8.8.4` | 25 | | Java | This is [documented behavior in Java], [since Java 1.0] | 26 | | Julia | https://news.ycombinator.com/item?id=30619661 | 27 | | Lua | `v5.3.3` | [Example](Examples.md#Lua) 28 | | Nim | `Nim Compiler Version 1.6.4` | 29 | | Node.js | `v12.21.0` | 30 | | Pascal | https://news.ycombinator.com/item?id=30613381 | 31 | | Python 2 | `Python 2.7.18` | 32 | | Ruby | `ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux-gnu]` | 33 | | Scheme | `CHICKEN 5.2.0` | 34 | | Swift | `Swift version 5.5.3` | 35 | 36 | ## Languages without the bug 37 | 38 | | Language | Notes 39 | | ---------- | --------------- 40 | | Ada | `GNAT 10.3.0` 41 | | Awk | `GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)` 42 | | Bash | `GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)` 43 | | C# | `Mono JIT compiler version 6.8.0.105` 44 | | Deno | `deno 1.11.0 (release, x86_64-unknown-linux-gnu)` 45 | | Erlang/OTP | `Erlang/OTP 26.0` or above 46 | | Hack | `HHVM 4.108` or above when using [HSL IO] 47 | | Hare | `hare` + `harec` 20230222 snapshot 48 | | Lisp | `SBCL 2.1.1` 49 | | OCaml | `4.08.1` 50 | | Perl | `perl 5, version 32, subversion 1 (v5.32.1) built for x86_64-linux-gnu-thread-multi (with 46 registered patches...)` 51 | | Perl 6 | `v2020.12` 52 | | Php | `PHP 8.0.8 (cli) (build: Mar 3 2022 14:51:53) ( NTS )` 53 | | PowerShell | https://twitter.com/perostergaard/status/1501936409547993102 54 | | Python 3 | `Python 3.9.5` 55 | | Racket | `Racket v7.9 [bc]` 56 | | Rust | This is [documented behavior in Rust]. 57 | | Tcl | `8.6.11` 58 | | Zig | https://ziglang.org/documentation/master/#Hello-World 59 | 60 | The specific source code tested comes from doing simple Web searches for 61 | hello world programs. The intention is to test the programs that are presented 62 | to newcomers, and not programs specifically written to fix this bug. 63 | 64 | In some cases, only a specific implementation has been tested, and it's not yet 65 | been determined what the actual documented or specified behavior is. In those 66 | cases, I've listed the versions tested, or links to reports. 67 | 68 | Node.js (and Deno) is listed here as a "Language" because JavaScript itself doesn't have 69 | a builtin concept of standard output, so it takes an embedding like Node.js to 70 | connect JS features to command-line concepts such as standard output. 71 | 72 | PRs adding more languages, more information about existing languages, updates, 73 | or corrections are welcome! 74 | 75 | [documented behavior in Java]: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/PrintStream.html 76 | [since Java 1.0]: http://web.mit.edu/java_v1.0.2/www/javadoc/java.io.PrintStream.html#checkError() 77 | [documented behavior in Rust]: https://doc.rust-lang.org/stable/std/macro.println.html#panics 78 | [Wikipedia's list of "Hello, World!" programs]: https://en.wikipedia.org/wiki/%22Hello,_World!%22_program#Examples 79 | [The Hello World Collection]: http://helloworldcollection.de/ 80 | [the original blog post]: https://blog.sunfishcode.online/bugs-in-hello-world/ 81 | [HSL IO]: https://docs.hhvm.com/hack/getting-started/input-and-output 82 | --------------------------------------------------------------------------------