Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf() and write()

Reply
Thread Tools

printf() and write()

 
 
ericunfuk
Guest
Posts: n/a
 
      03-10-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-10-2007
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.
 
Reply With Quote
 
 
 
 
Nelu
Guest
Posts: n/a
 
      03-10-2007
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...)
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-10-2007
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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-10-2007
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.

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-10-2007
On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
<(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-10-2007
"Mark McIntyre" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
> <(E-Mail Removed)> wrote:
>
>>printf("hello");

>
> try printf("hello\n");

try is C++

Sorry, could not resists...

Bye, Jojo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments