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

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top