ericunfuk wrote:
printf("hello");
write(1,"hello",5);
Are these two have the same effect?Only the 2nd one work for me
sometimes?Are there situations that I can only use write() instead of
printf()?
write is not part of the C standard.
What do you mean by "Only the 2nd one work for me"?
printf("hello") should work as long as you have a correctly
written C program that include <stdio.h>. It may not print
anything on the screen but that's because you need to add a new
line so the output stream gets flushed.
Try these two programs and let me know which one works:
P1:
#include <stdio.h>
int main(void) {
printf("hello");
return 0;
}
P2:
#include <stdio.h>
int main(void) {
printf("hello\n");
return 0;
}
The standard C functions use buffered I/O operations so there are
no guarantees that anything will be output on the screen until
the stdio buffer is flushed.
The fwrite function is similar to printf but there are no
guarantees you will see anything on the screen until the buffer
is flushed.
<OT>
I don't think the write function is buffered (I may be wrong
though and you should ask about the function in a POSIX group) so
what you write shows up on the screen right away.
</OT>
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)