$ 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)
}
}