tags:

views:

53

answers:

1
int main ( )
{
    char C[] = "Hello World";
    write(0,C,sizeof(C));
    return 0;
}

In the above program, I am writing to File descriptor ZERO which I suppose by default is STDIN.. Then why I am I getting output at STDOUT?

shadyabhi@shadyabhi-desktop:~$ ./a.out
Hello Worldshadyabhi@shadyabhi-desktop:~$
+5  A: 

Standard input is for reading, not writing. What happens when you write to standard input (or read from standard output) is unspecified. Here, both standard input and standard output point to the pseudo-terminal into which the application runs, and the terminal emulator did not take care to make the '0' descriptor "read-only". Hence, the kernel does not prevent writing to standard input, and it goes to the pseudo-terminal just as if it was written to standard output.

For portability, you should not rely on such behaviour.

Thomas Pornin
Cool answer! Did not know this!
Matt Joiner