writing a string to output

J

junky_fellow

Hi guys,

I want to write a null terminated string to the standard output. I
don't want to use
printf as it may be costly. I am using "puts" library function, but it
appends the
newline to the end of string which I do not want. Is there any library
function which
does not append the newline character ?

Also, any idea why does puts appends newline to the input string ?

There's another library function "fputs" that does not append the
newline character.
I was wondering why puts appends newline but fputs does not ? Is there
any special
reason for this ?

thanks a lot for any help in advance ...
 
I

Ian Collins

Hi guys,

I want to write a null terminated string to the standard output. I
don't want to use
printf as it may be costly.

Have you measured it and found it too costly for you application?

I am using "puts" library function, but it
appends the
newline to the end of string which I do not want. Is there any library
function which
does not append the newline character ?

Also, any idea why does puts appends newline to the input string ?
I'm afraid the answer is "because that's what the standard says they do"
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Ian said:
Have you measured it and found it too costly for you application?

I am using "puts" library function, but it
I'm afraid the answer is "because that's what the standard says they do"

The standard also provides fputs, which the OP might find useful.
 
C

Cong Wang

Hi guys,

I want to write a null terminated string to the standard output. I
don't want to use
printf as it may be costly. I am using "puts" library function, but it
appends the
newline to the end of string which I do not want. Is there any library
function which
does not append the newline character ?

Also, any idea why does puts appends newline to the input string ?

There's another library function "fputs" that does not append the
newline character.
I was wondering why puts appends newline but fputs does not ? Is there
any special
reason for this ?

thanks a lot for any help in advance ...

`puts' is the most convenient function for printing simple messages.
For example:

puts ("This is a message.");

outputs the text `This is a message.' followed by a newline.

So I think the reason is conveniency.
 
R

Rod Pemberton

Hi guys,

I want to write a null terminated string to the standard output. I
don't want to use
printf as it may be costly. I am using "puts" library function, but it
appends the
newline to the end of string which I do not want.

There's another library function "fputs" that does not append the
newline character.

You answered your own question: 'fputs' to FILE 'stdout'.

If you still need the formatting ability, you can use 'sprintf' and 'fputs'.
Many implementations have a single core routine for the various *printf
functions. You'll need to check to see how much is actually saved with
'fputs' with 'sprintf' versus 'printf'.

However, you may be able to reduce things further. Many 'fputs' routines
add buffering and output the string using a 'putc' while loop. If you don't
need the buffering, write your own.


Rod Pemberton
 
M

Michal Nazarewicz

I want to write a null terminated string to the standard output. I
don't want to use printf as it may be costly.

Honestly I doubt it that it is costly even though I prefer using
puts()/fputs() to printf()/fprintf() when it's possible.
I am using "puts" library function, but it appends the newline to
the end of string which I do not want. Is there any library function
which does not append the newline character ?

fputs, like: fputs(string, stdout);
Also, any idea why does puts appends newline to the input string ?

Because the standard says so.
There's another library function "fputs" that does not append the
newline character.

So after all you already knew the answer to the former question.
I was wondering why puts appends newline but fputs does not ? Is
there any special reason for this ?

Yes - C standard.
 
A

Ancient_Hacker

You may have a misplaced interest in efficiency. The physical I/O to
standard output, whether it be a TTY, a virtual TTY, or redirected to a
file, is likely to be the bottleneck, not the small overhead in calling
printf(). If you step thru the code, from entering printf(), to it
seeing the "%s" format spec, to moving the string to the output buffer,
is well under 100 instructions.
That's much less time than what it takes to write to a console or a
disk.
 
P

pete

"puts" library function
Is there any library function which
does not append the newline character ?
There's another library function "fputs" that does not append the
newline character.

That's what I was thinking.

I think of puts as converting strings into lines.
Strings in memory are somewhat analagous to lines of text in a file.
Strings are '\0' terminated and lines are '\n' terminated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top