470,631 Members | 1,994 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

fprintf formatting question???

I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:

n = 12;
fprintf( stdout, "%(n)s: ", string );

I get: %(n)s:

Any ideas?

---John

Nov 14 '05 #1
7 1292
Greetings,

jc**********@gmail.com wrote:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:

n = 12;
fprintf( stdout, "%(n)s: ", string );


fprintf( stdout, "%*s: ", n, string);

--
Kyle A. York
Sr. Subordinate Grunt
Nov 14 '05 #2
"jc**********@gmail.com" <jc**********@gmail.com> writes:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:

n = 12;
fprintf( stdout, "%(n)s: ", string );


printf("%*s: ", n, string);
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Nov 14 '05 #3
Thanks much! ---John

Nov 14 '05 #4
On Fri, 20 May 2005 12:45:42 -0700, kyle york wrote:
Greetings,

jc**********@gmail.com wrote:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:
No, you will get leading blanks added to make the total length of the
field up to 12. You would only get 12 blanks added if string has zero
length.

n = 12;
fprintf( stdout, "%(n)s: ", string );


fprintf( stdout, "%*s: ", n, string);


Note that the argument passed for * muts have type int. Cast if n has some
other type.

Lawrence

Nov 14 '05 #5
Strange thing happened on the way to the forum:

fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
get_entity_id( child, level+1 ) );

produces no leading blanks (spaces),

while:

fprintf( stderr, "%*s %s name: %s: \n", 6*level, "Node: ", child->name,
attr );

produces the expected/require leading blanks (spaces).

I checked the value of 'level' in the first fprintf and it was what I
expected (3). One other observation: if I replace the first fprintf
statement with the second (in the same routine) - it works as expects:
it adds the leading spaces.

Any thoughts?

Nov 14 '05 #6
In article <11**********************@g44g2000cwa.googlegroups .com>
jc**********@gmail.com <jc**********@gmail.com> wrote:
fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
get_entity_id( child, level+1 ) );
Note: strlen("Target Entity ID: ") is 18.
produces no leading blanks (spaces), while:

fprintf( stderr, "%*s %s name: %s: \n", 6*level, "Node: ", child->name,
attr );

produces the expected/require leading blanks (spaces).
Note: strlen("Node: ") is 6.

Next, we have:
I checked the value of 'level' in the first fprintf and it was what I
expected (3).


Assuming "level" has type int (or promotes to type int), 6*level will
be 6*3, i.e., the "int" value 18.

printf("[%18s]", "123456789012345678");
printf("[%18s]", "123456");

should cause the square brackets to line up, because the first
string is 18 characters long and printed in an 18 character field,
while the second string is 6 characters long and printed in an 18
character field. The field width tells printf() to pad (with
spaces, in this case) as needed to make sure the output is at least
that long. Since 18 minus 18 is zero, the first printf() requires
no extra padding characters.

(If "level" has type long, or double, or similar, all bets are off,
because "%*s" needs one (int) and one (char *). I suspect this is
not a problem, though.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40į39.22'N, 111į50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #7
Got it, thanks! ---John

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by hpy_awad | last post: by
3 posts views Thread by Andrew Fabbro | last post: by
4 posts views Thread by GGarramuno | last post: by
2 posts views Thread by Joy2006 | last post: by
4 posts views Thread by grimrob | last post: by
3 posts views Thread by vamsi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.