fprintf formatting question???

J

jchludzinski

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
 
K

kyle york

Greetings,

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);
 
B

Ben Pfaff

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);
 
L

Lawrence Kirby

Greetings,

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.
fprintf( stdout, "%*s: ", n, string);

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

Lawrence
 
J

jchludzinski

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?
 
C

Chris Torek

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.)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top