printf() width specifier in text fields | | |
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 | | | | re: printf() width specifier in text fields
MikeC wrote, On 22/07/07 11:29: Quote:
Folks,
>
Can the width specifier be used in a printf() text string?
>
If I execute...
>
printf("%4s", ".........."); it prints ten dots.
printf("%4.4s",".........."); Quote:
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 Quote:
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. Quote:
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.
--
Flash Gordon | | | | re: printf() width specifier in text fields
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
"Flash Gordon" <spam@flash-gordon.me.ukwrote in message
news:ejpbn4x457.ln2@news.flash-gordon.me.uk... Quote:
MikeC wrote, On 22/07/07 11:29: Quote:
>Folks,
>>
>Can the width specifier be used in a printf() text string?
>>
>If I execute...
>>
>printf("%4s", ".........."); it prints ten dots.
>
printf("%4.4s","..........");
> Quote:
>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
> Quote:
>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.
> Quote:
>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.
--
Flash Gordon
| | | | re: printf() width specifier in text fields
MikeC wrote: Quote:
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 */
--
pete | | | | re: printf() width specifier in text fields
Thanks Pete,
.... but I think you missed the point.
Regards,
MikeC
"pete" <pfiland@mindspring.comwrote in message
news:46A35151.5E61@mindspring.com... Quote:
MikeC wrote:
> Quote:
> If I execute....
> Quote:
>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 */
>
--
pete
| | | | re: printf() width specifier in text fields
MikeC wrote: Quote:
"pete" <pfiland@mindspring.comwrote in message
news:46A35151.5E61@mindspring.com...
> Quote:
>>MikeC wrote:
>> Quote:
>>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 */
>>
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".
--
Thad | | | | re: printf() width specifier in text fields
MikeC wrote: Quote:
>
Thanks Pete,
>
... but I think you missed the point.
I disagree. Quote:
"pete" <pfiland@mindspring.comwrote in message
news:46A35151.5E61@mindspring.com... Quote:
MikeC wrote: Quote:
printf("%4s", "."); it prints a dot (no spaces).
What you wrote is wrong: Quote:
printf("%4s", "."); it prints a dot (no spaces).
No. It prints 3 spaces followed by a dot. Quote:
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. Quote:
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", "."); Quote:
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 */
--
pete |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|