By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

printf() and write()

P: n/a
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()?
Thanks

Mar 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 isn't standard C, its POSIX. You should use fwrite if you want
your code to be portable. You would use (f)write to write to a stream
other than the standard output.

--
Ian Collins.
Mar 10 '07 #2

P: n/a
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...)
Mar 10 '07 #3

P: n/a
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()?
There is no standard C function called 'write'. There is no way to tell
what your implementation defines 'write' to mean without seeing that
implementation's documentation. Draw your own conclusion.
Mar 10 '07 #4

P: n/a
ericunfuk wrote:
printf("hello");
write(1,"hello",5);

Are these two have the same effect?
Depends on the definition of write, which is not defined by the C
Standard. There's a POSIX function by that name though.
Only the 2nd one work for me sometimes?
Any such limitation is likely to involve details of system specific
nature. You should pursue that angle in a POSIX, UNIX or system
specific group like comp.unix.programmer.

Mar 10 '07 #5

P: n/a
On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
<xu***********@gmail.comwrote:
>printf("hello");
try printf("hello\n");

Without a newline, printf is not guaranteed to flush the output buffer
to screen, so nothing may appear.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 10 '07 #6

P: n/a
"Mark McIntyre" <ma**********@spamcop.netschrieb im Newsbeitrag
news:m7********************************@4ax.com...
On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
<xu***********@gmail.comwrote:
>>printf("hello");

try printf("hello\n");
try is C++

Sorry, could not resists...

Bye, Jojo
Mar 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.