473,608 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7015
"Adrian Neumeyer" <an*******@yaho o.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.ne t
"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**********@t iscali.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**********@t iscali.no> wrote:
Thomas Pfaff <th**********@t iscali.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*****@freene t.de> wrote:
Thomas Pfaff <th**********@t iscali.no> wrote:
Thomas Pfaff <th**********@t iscali.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.ne t
Nov 13 '05 #9
<grab> Added to the list of interview questions </grab>

Thanks!
--
#include <standard.discl aimer>
_
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
2308
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 strings, while I always keep them inline. Out of curiosity, I wrote the following (with some help from the online php man page on microtime()) to see which was more efficient. Not only is my way (in-line) worse, it's *much* worse. If anyone has a...
7
1951
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 %s" % myvalues()
4
1994
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 format strings?
11
5930
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 <= 4 */ };
0
996
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
4465
by: Dennis Benzinger | last post by:
Is there a library with a strftime replacement which supports Unicode format strings? Bye, Dennis
9
1726
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 arg') and it doesn't work but I do see examples with % that is borrowed from the c language. Seems like if I can write a batch file that does something the same behavior should happen in the os module..
7
1551
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 is the amazing flexibility of our brains. For example, if a block of text isn't arranged in the order we're used to, we can easily rearrange it mentally and read it anyway. Oriental and Arabic peoples, for example, do this each time they read...
7
18565
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 show a specific number of significant digits to the right of the decimal place with a format string? NOT number of decimal places, significant digits. For example;
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1328
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.