473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

printf() and write()

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
6 6272
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
5
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
11
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab,...
2
by: rameshbabupeddapalli | last post by:
Hi, I want to create my own variable length parameters function it should work like printf() function. When i write the program as given below it's working fine #include<stdio.h> ...
43
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.