sprintf

M

Magix

Hi,
How can I sprintf str1 str2 and str3 at certain position.
sprintf(szBuffer, "%s %s %s", str1, str2, str3);

example:
Sandy 4001 New York City
Joe 500001 California
Rebecca 888 Michigan
 
B

Barry Schwarz

Hi,
How can I sprintf str1 str2 and str3 at certain position.
sprintf(szBuffer, "%s %s %s", str1, str2, str3);

example:
Sandy 4001 New York City
Joe 500001 California
Rebecca 888 Michigan
In your reference, review the section about length specifications that
can be used with the %s format specification.


<<Remove the del for email>>
 
M

Magix

I couldn't find any info. If u have any URL that provide relevant info,
please let me know.
 
C

CBFalconer

*** top-posting fixed ***
Magix said:
I couldn't find any info. If u have any URL that provide relevant
info, please let me know.

Look in your C book. Also google for N869 or try the dinkum pages.

Meanwhile, don't toppost. Topposters make us mean and angry, and
we become most unpleasant.
 
T

Thomas Matthews

Magix said:
Hi,
How can I sprintf str1 str2 and str3 at certain position.
sprintf(szBuffer, "%s %s %s", str1, str2, str3);

example:
Sandy 4001 New York City
Joe 500001 California
Rebecca 888 Michigan

Try %#s where # is the field width:
printf("%-10s%6d %-20s",
"Sandy", 4001, "New York City\n");

Look up in your C reference book the term
"format specifiers". If you don't have
a book get one. If your book doesn't have
this term in the index, get a different book.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
M

Magix

Thomas Matthews said:
Try %#s where # is the field width:
printf("%-10s%6d %-20s",
"Sandy", 4001, "New York City\n");

Look up in your C reference book the term
"format specifiers". If you don't have
a book get one. If your book doesn't have
this term in the index, get a different book.

I know "-" means left-aligned, "+" means right-aligned.
Does %-10s mean fixed 10 character spac, allocated for that string ?
I tried in my case sprintf(szBuffer, "%-30s %s", str1, str2), but noticed
that if str1 is more than 12 char long, the str2 will not be aligned.
example:
TestingOut1 Hello
TestingOut123 World
 
T

Tor Rustad

I know "-" means left-aligned, "+" means right-aligned.

No, "+" flag is not relevant for the %s conversion. "+" flag
make printf show the sign (+ or -). "-" flag means left justified,
while the default is right justified.
Does %-10s mean fixed 10 character spac, allocated for that string ?

No. 10 means minimum field width.

"Characters from the array are written up to (but not including) the
terminating null character. If the precision is specified, no more than
that many characters are written."

I tried in my case sprintf(szBuffer, "%-30s %s", str1, str2), but noticed
that if str1 is more than 12 char long, the str2 will not be aligned.
example:
TestingOut1 Hello
TestingOut123 World

Well, let see

C:\Temp>type test.c
#include <stdio.h>

int main( void )
{
printf("%-30s %s\n", "TestingOut1", "Hello");
printf("%-30s %s\n", "TestingOut123", "World");

return 0;
}

C:\Temp>cl /Za test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for
80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:test.exe
test.obj

C:\Temp>test
TestingOut1 Hello
TestingOut123 World

which was displayed _exactly_ as expected. Perhaps try to watch
it in Courier font...
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top