variables in format strings -- printf

A

Adrian Neumeyer

Hello folks,

I have a simple question:

I want to control the indentation of strings printed out
with printf(...). Not just a fixed indentation, but lets say
one blank character more for each new line, so that
a staircase effect is produced.

Example:
One
Two
Three
....

I was not able to create a working format string for
printf().

Can somebody help me?

Thanks!

Adrian
 
M

Martin Dickopp

Adrian Neumeyer said:
I want to control the indentation of strings printed out
with printf(...). Not just a fixed indentation, but lets say
one blank character more for each new line, so that
a staircase effect is produced.

Example:
One
Two
Three
....

I was not able to create a working format string for
printf().

#include <stdio.h>

int main (void)
{
const char *const strings [] = {"One", "Two", "Three"};
size_t i;

for (i = 0; i < sizeof strings / sizeof strings [0]; ++i)
printf ("%*s%s\n", (int)i, "", strings );

return 0;
}

"%*s" expects two arguments, an `int' and a pointer to a string. The
former specifies the minimum width for the latter. The trick here is
to output an empty string, so that only padding spaces are written.

Martin
 
N

nrk

Adrian said:
Hello folks,

I have a simple question:

I want to control the indentation of strings printed out
with printf(...). Not just a fixed indentation, but lets say
one blank character more for each new line, so that
a staircase effect is produced.

Example:
One
Two
Three
....

I was not able to create a working format string for
printf().

printf("%*s\n", width, string);

where width is the field width of the string printed, and can be computed
using the length of the string to be printed and whatever lead space you
want for your staircase effect.

HTH,
nrk.
 
J

Joe Wright

Adrian said:
Hello folks,

I have a simple question:

I want to control the indentation of strings printed out
with printf(...). Not just a fixed indentation, but lets say
one blank character more for each new line, so that
a staircase effect is produced.

Example:
One
Two
Three
....

I was not able to create a working format string for
printf().

Can somebody help me?

Thanks!

Adrian
#include <stdio.h>

int main(void) {
int i;
for (i = 0; i < 5; ++i)
printf("%*s%s\n", i, "", "Adrian");
return 0;
}
 
M

Martin Ambuhl

Adrian Neumeyer wrote:

I want to control the indentation of strings printed out
with printf(...). Not just a fixed indentation, but lets say
one blank character more for each new line, so that
a staircase effect is produced.

Example:
One
Two
Three
....

I was not able to create a working format string for
printf().

printf("%*s%s\n",currentindent,"",texttoprint);
 
T

Thomas Pfaff

Martin Dickopp said:
#include <stdio.h>

int main (void)
{
const char *const strings [] = {"One", "Two", "Three"};

Note that the identifiers str[a-z]* is reserved for use by the
implementation, thus the name `strings' violate the Standard.
 
T

Thomas Pfaff

Thomas Pfaff said:
Martin Dickopp said:
#include <stdio.h>

int main (void)
{
const char *const strings [] = {"One", "Two", "Three"};

Note that the identifiers str[a-z]* is reserved for use by the
implementation, thus the name `strings' violate the Standard.

Or wait, did that only apply to identifiers with external linkage?
 
I

Irrwahn Grausewitz

Thomas Pfaff said:
Thomas Pfaff said:
Martin Dickopp said:
#include <stdio.h>

int main (void)
{
const char *const strings [] = {"One", "Two", "Three"};

Note that the identifiers str[a-z]* is reserved for use by the
implementation, thus the name `strings' violate the Standard.

Or wait, did that only apply to identifiers with external linkage?
Yes.

From n48:

7.26 Future library directions

[#1] [...] All external names described below
are reserved no matter what headers are included by the
program.


7.26.11 String handling <string.h>

[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.h> header.

Regards

Irrwahn
 
D

Dave Thompson

Thomas Pfaff said:
Thomas Pfaff said:
#include <stdio.h>

int main (void)
{
const char *const strings [] = {"One", "Two", "Three"};

Note that the identifiers str[a-z]* is reserved for use by the
implementation, thus the name `strings' violate the Standard.

Or wait, did that only apply to identifiers with external linkage?
Yes.

From n48:
IGYM n843, and even if you can't/won't get the actual standard you
might as well move up to n869, it's equally free.
7.26 Future library directions

[#1] [...] All external names described below
are reserved no matter what headers are included by the
program.
And a local (ordinary) identifier does not conflict with an external
name, so it's OK on this front.
7.26.11 String handling <string.h>

[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.h> header.
And all library functions can be macros, so this effectively reserves
the names for any use, *if* you don't explicitly #undef after
#include'ing <string.h>, which the example didn't -- but in a real
program might be hidden in another header or added at any time, so to
be absolutely safe you should just steer clear.

- David.Thompson1 at worldnet.att.net
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top