Why is this working the way it is?

In C, when I'm trying to write:

write(1,buf,sizeof(buf));

or

write(0,buf,sizeof(buf));

it is working... As an interesting side note, on the Ubuntu machines, writing to 0 and 1 appears to produce the same output. Can someone explain why this is happening irrespective of 1 or 0?

When a program starts it traditionally starts with three file descriptors

0 = stdin
1 = stdout
2 = stderr

So at bare minimum your program has

(a) command line arguments
(b) the environment variables
(c) the current directory
(d) a byte stream on stdin

As a result of it's processing it can

(e) write normal output to stdout
(f) write errors to stderr

However if a process is connected to a terminal, or launched by inetd, those three file descriptors *may* all refer to exactly the same file. If you follow the rules, you can still read from stdin, and write to stdout/stderr.

The fun starts when you start redirecting the input/output of a program, for example the shell will do this with "|", ">", "<" etc.

No wonder it was working so perfectly... So this means that I was writing a program that did nothing special except printing out some characters and so everything was the same in this particular situation. Thank you so much for the explanation...