473,379 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

variables in format strings -- printf

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
Nov 13 '05 #1
9 7005
"Adrian Neumeyer" <an*******@yahoo.de> writes:
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 [i]);

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
Nov 13 '05 #2
Adrian Neumeyer wrote:

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;
}

--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #3
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);
--
Martin Ambuhl

Nov 13 '05 #4
nrk
Adrian Neumeyer wrote:
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.
Can somebody help me?

Thanks!

Adrian


Nov 13 '05 #5
Martin Dickopp <ex*************@zero-based.org> writes:
#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.
Nov 13 '05 #6
Thomas Pfaff <th**********@tiscali.no> writes, again and again:
Martin Dickopp <ex*************@zero-based.org> writes:
#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?
Nov 13 '05 #7
Thomas Pfaff <th**********@tiscali.no> wrote:
Thomas Pfaff <th**********@tiscali.no> writes, again and again:
Martin Dickopp <ex*************@zero-based.org> writes:
> #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
--
What does this red button do?
Nov 13 '05 #8
On Tue, 16 Sep 2003 10:28:32 +0200, Irrwahn Grausewitz
<ir*****@freenet.de> wrote:
Thomas Pfaff <th**********@tiscali.no> wrote:
Thomas Pfaff <th**********@tiscali.no> writes, again and again:
Martin Dickopp <ex*************@zero-based.org> writes:
> #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
Nov 13 '05 #9
<grab> Added to the list of interview questions </grab>

Thanks!
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Bryant | last post by:
(N.B. - this may be a well-known thing, but it was new to me). I was reading a script written by someone else, and noticed a coding style difference - they always concatenated variables into...
7
by: marduk | last post by:
I have a weird request. I want to be able to say def myvalues(): while True: # stuff that determines a new somevalue yield somevalue x = "Hello, %s, this is a %s with %s and %s on top of...
4
by: grv575 | last post by:
For a string like print '%.*f' % (prec_length, float_var) How does python convert this to a C printf sytle format string? Is there a way to specify the precision length for floating points in C...
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
0
by: Mike | last post by:
can anyone explain to me why, given: decimal dec = 84.50M; Convert.ToInt32(dec).ToString() and Math.Round(dec,0).ToString() both correctly output "84"
1
by: Dennis Benzinger | last post by:
Is there a library with a strftime replacement which supports Unicode format strings? Bye, Dennis
9
by: Eric_Dexter | last post by:
http://www.ddj.com/184405774;jsessionid=BDDEMUGJOPXUMQSNDLQCKHSCJUNN2JVN I saw a warning from homeland security about this. I only comment on the because I am trying to use os.system('command1...
7
by: Carroll, Barry | last post by:
Greetings: Personally, I don't think top-posting is the most annoying newsgroup habit. I think it's making a big fuss about minor inconveniences. One of the nicest things about being human...
7
by: Michael Howes | last post by:
MSDN documentation for format strings seems ambiguous. It's says things like "significant digits or number of decimal places" all over the place. Well those two things are different. How do I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.