|
|
|
|
|
by usrbinbash
1560 days ago
|
|
On my machine: $ cat main.c
#include <stdio.h>
int main(void) {
int n = printf("Hello, World!\n");
return n < 0 ? 1 : 0;
}
$ cc main.c
$ a.out
Hello, World!
$ echo $?
0
$ a.out > /dev/full
$ echo $?
0
Problem is, printf is buffered. When redirecting to /dev/full writing to the buffer works. The problem only manifests when the buffer is flushed.This works, as write is not buffered; #include <unistd.h>
int main(void) {
int n = write(1, "Hello, World!\n", 14);
return n < 0 ? 1 : 0;
}
But even then, to find out what exactly went wrong, we would have to check errno.It's easier in Go, because fmt functions do have an error return which will report such errors; func main() {
_, err := fmt.Println("Hello, World!")
if err != nil {
os.Exit(1)
}
}
|
|
In any case, hello world examples are used for two purposes, (a) providing a simplest possible program that you can run (with an indication that it worked), and (b) to give a taste of the respective programming language. At least for the latter case, it would be useful to demonstrate the necessary error handling, if any. The above is just showing that this is not entirely trivial in C with its standard library.