Print Format

J

joriveek

Hi,

I want to print a string with defined width left trailed; How can I do
this?

Example: Print ("%05d%3s", &test1, test2);

say test1 = 23
test2 = Hi

It prints like 1 2 3 4 5 6 7 8
0 0 0 2 3 H i
But I want like this:
It prints like 1 2 3 4 5 6 7 8
0 0 0 2 3 H i

How can I do this?

Thanks
 
M

Martin Ambuhl

I want to print a string with defined width left trailed; How can I do
this?
Example: Print ("%05d%3s", &test1, test2);


Please note that "printf" is not spelled "Print" and that the '&'
preceding test1 is wrong.
say test1 = 23
test2 = Hi

It prints like 1 2 3 4 5 6 7 8
0 0 0 2 3 H i
But I want like this:
It prints like 1 2 3 4 5 6 7 8
0 0 0 2 3 H i

But neither of those is what it prints, is it?
Those extra spaces may make things clearer to you, but they get in the
way of a clearly stated question.
How can I do this?

#include <stdio.h>

int main(void)
{
char rule[] = "0000000001111111111\n"
"1234567890123456789\n", teststr[] = "Hi";
int testnum = 23;

printf("%s%06d%13s|\n\n", rule, testnum, teststr);
printf("%s%06d%-13s|\n\n", rule, testnum, teststr);
return 0;
}


0000000001111111111
1234567890123456789
000023 Hi|

0000000001111111111
1234567890123456789
000023Hi |
 

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,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top