simple printf question

B

Bint

Hi,

I have a giant string buffer, and I want to print out small chunks of it at
a time. How do I print out, say 20 characters of a string?

Is it like this?

printf("%20s",mystring);

I can change the start point of the string, I just don't know how to tell it
to only print out X number of characters from it.
Thanks
B
 
R

Richard Heathfield

Bint said:
Hi,

I have a giant string buffer, and I want to print out small chunks of it
at
a time. How do I print out, say 20 characters of a string?

Is it like this?

printf("%20s",mystring);

I can change the start point of the string, I just don't know how to tell
it to only print out X number of characters from it.

printf("%.20s", mystring);

Note the dot in the format specifier.

Covered in K&R2 p244.
 
T

Tor Rustad

Bint said:
Hi,

I have a giant string buffer, and I want to print out small chunks of it at
a time. How do I print out, say 20 characters of a string?

Is it like this?

printf("%20s",mystring);

printf("%.*s", len, mystring);
 
H

husterk

Hi,

I have a giant string buffer, and I want to print out small chunks of it at
a time. How do I print out, say 20 characters of a string?

Is it like this?

printf("%20s",mystring);

I can change the start point of the string, I just don't know how to tell it
to only print out X number of characters from it.
Thanks
B

Bint,

Another more flexible method would be to snprintf() your larger string
into a temporary string buffer and then output the temporary buffer
using a standard unformatted printf(). This will allow you to
dynamically change the size of your output string by supplying a
variable length for your temporary string buffer in the snprintf()
routine (which cannot be accomplished using the "%.20s" method
described by Richard.

Keith
http://www.doubleblackdesign.com
 
R

Richard Heathfield

husterk said:

Another more flexible method would be to snprintf() your larger string
into a temporary string buffer and then output the temporary buffer
using a standard unformatted printf(). This will allow you to
dynamically change the size of your output string by supplying a
variable length for your temporary string buffer in the snprintf()
routine (which cannot be accomplished using the "%.20s" method
described by Richard.

There is really no need to go to all that trouble.

printf("%.*s\n", nchars, mystring);

Again, this is documented very clearly in K&R2.
 
C

Charlie Gordon

Richard Heathfield said:
husterk said:



There is really no need to go to all that trouble.

printf("%.*s\n", nchars, mystring);

Again, this is documented very clearly in K&R2.

Be careful to cast nchar as (int) if it has a different type, such as
size_t.
 

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

Similar Threads

Question on printf with %x 8
Any Ideas On This Simple Co-Prime program 1
printf question 4
Problem with simple pthread program in C 1
printf and print 2
!printf() 8
printf magic? 2
POSIX enhancements to printf 95

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top