printf() width specifier in text fields

M

MikeC

Folks,

Can the width specifier be used in a printf() text string?

If I execute...

printf("%4s", ".........."); it prints ten dots.

If I execute

printf("%4s", "."); it prints one dot.

I was hoping to use sprintf to fill in a number of spaces in a field, such
as...

int spaces = 6;

sprintf(buf, "head %*s tail", spaces, " ");

If printf() can't do it, I know I can write a function that will handle it,
but is there a more elegant way? printf() would be attractive because I
would be able to fill in "head", "tail" and a given number of spaces between
them, all in one statement.

Thanks,

MikeC

--
Mental decryption required to bamboozle spam robots:

mike_best$ntlworld*com
$ = @
* = dot
 
F

Flash Gordon

MikeC wrote, On 22/07/07 11:29:
Folks,

Can the width specifier be used in a printf() text string?

If I execute...

printf("%4s", ".........."); it prints ten dots.
printf("%4.4s","..........");

If I execute

printf("%4s", "."); it prints one dot.

Are you sure it does not print 3 spaces and 1 dot? Try
printf('"%4s'", ".");
so you can see
I was hoping to use sprintf to fill in a number of spaces in a field, such
as...

int spaces = 6;

sprintf(buf, "head %*s tail", spaces, " ");

I'm not entirely sure what you want. Possibly %*.*s specifying minimum
and maximum length.
If printf() can't do it, I know I can write a function that will handle it,
but is there a more elegant way? printf() would be attractive because I
would be able to fill in "head", "tail" and a given number of spaces between
them, all in one statement.

You can probably do what you want. The number before the decimal point
is minimum width, after is maximum width, so both the same specifies
exact width. You can also use negative numbers to specify left justified
if that is what you want.
sprintf(buf, "%*.*s %*.*", -7,7,"head",-7,7,"tail");
Or whatever.
 
M

MikeC

Thanks a million, Flash,

I learned something I didn't know from your post (that you can specify max
and min widths), but I independently found the answer to what I was looking
for by Googling around the web.

What I was missing was the + or - specifiers in the field. If I execute....

printf("%4s", "."); it prints a dot (no spaces). If I execute
printf("%-4s", "."); it prints three spaces and one dot (field width of 4).
If I execute
printf("%+4s", "."); it puts the dot at the other end of the field of 4 (a
dot, then three spaces)
If I replace the dot with a space, and execute
printf("%-4s", " "); it gives me three spaces followed by a space, which
is exactly what I wanted.

Thanks for your reply all the same - as I said, I learned something!

MikeC
 
P

pete

MikeC wrote:
If I execute....
printf("%4s", "."); it prints a dot (no spaces).

Try it this way and see what happens:

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
printf("%4s", ".");
printf("%4s", ".");
printf("%4s", ".");
printf("%4s", ".");
printf("%4s", ".");
putchar('\n');
return 0;
}

/* END new.c */
 
P

pete

MikeC said:
Thanks Pete,

... but I think you missed the point.

I disagree.

What you wrote is wrong:
printf("%4s", "."); it prints a dot (no spaces).

No. It prints 3 spaces followed by a dot.
If I execute printf("%-4s", ".");
it prints three spaces and one dot (field width of 4).

No. It prints a dot followed by 3 spaces.
If I execute printf("%+4s", ".");
it puts the dot at the other end of the field of 4
(a dot, then three spaces)

No. It does the exact same thing as printf("%4s", ".");
If I replace the dot with a space, and execute
printf("%-4s", " ");
it gives me three spaces followed by a space, which
is exactly what I wanted.

It gives you a space followed by three spaces,
which is what you want,
but if you knew what you were talking about,
eventually you would have gotten to this expression:

printf("%4s", " ")

Try it again:

/* BEGIN new.c output */
.X
.. X
.X
X
X
/* END new.c output */


/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
puts("/* BEGIN new.c output */");
printf("%4s", ".");
puts("X");
printf("%-4s", ".");
puts("X");
printf("%+4s", ".");
puts("X");
printf("%-4s", " ");
puts("X");
printf("%4s", " ");
puts("X");
puts("/* END new.c output */");
return 0;
}

/* END new.c */
 
T

Thad Smith

MikeC said:
Thanks Pete,

... but I think you missed the point.

Pete's point was to correct a misunderstnading you have about printf
specifiers. Understnnding that, you can get what you want with

printf ("foo%*sbar", nspaces, "");

to insert nspaces space characters between "foo" and "bar".
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top