linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Satchell <list@fluent2.pyramid.net>
To: Glynn Clements <glynn.clements@virgin.net>,
	"J." <mailing-lists@xs4all.nl>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: printf(), aligning fields
Date: Sat, 23 Aug 2003 18:05:56 -0700	[thread overview]
Message-ID: <5.2.1.1.0.20030823180113.011bceb8@fluent2.pyramid.net> (raw)
In-Reply-To: <16200.462.367759.211524@cerise.nosuchdomain.co.uk>

At 01:07 AM 8/24/2003 +0100, Glynn Clements wrote:
> > I am trying to print collums from a c program, as the
> > following example illustrates:
> >
> >   Total  Number   Folder
> >   -----  ------   ------
> >   15502   17      cronlog
> >   189897  42      linux/debian-curiosa
> >   161751  32      linux/debian-firewall
> >    4305   1       linux/debian-general
> >   17431   1       linux/debian-news
> >   107517  1       linux/vger-kernel-announce
> >   61136   16      linux/vger-kernel-c-programming
> >    8580   2       linux/vger-kernel-gcc
> >
> > How can I can align the output to the max right field boundry without
> > overcrossing that field boundry if the field value gets longer?
> >
> > Do I really have to check the length of the value before printing it with,
> > %*s, and then determine how many spaces I need?
>
>You have to make the field large enough to hold all of the values. The
>*printf() family of functions don't provide any way to truncate a
>field to a maximum width.

No, but a program can truncate numbers to the left.

printf ("%7d%3d %1.64s\n", (total % 10000000), (number % 1000), folder);

Your other option is to do a conversion to string, and fill the string with 
an "overflow indicator" when the number to be displayed is too large.  The 
advantage of the second method is you can insert comma (or dot, for Europe) 
delimiters to make the numbers easier for humans to read.


--
X -> unknown; Spurt -> drip of water under pressure
Expert -> X-Spurt -> Unknown drip under pressure.


  reply	other threads:[~2003-08-24  1:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-21 12:48 Question about checksums Holger Kiehl
2003-08-21 13:20 ` Luciano Miguel Ferreira Rocha
2003-08-21 16:36   ` Holger Kiehl
2003-08-21 17:28     ` Jeff Woods
2003-08-22 20:18       ` Holger Kiehl
2003-08-23 20:31         ` printf(), aligning fields J.
2003-08-24  0:07           ` Glynn Clements
2003-08-24  1:05             ` Stephen Satchell [this message]
2003-08-21 18:19     ` Question about checksums Luciano Miguel Ferreira Rocha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5.2.1.1.0.20030823180113.011bceb8@fluent2.pyramid.net \
    --to=list@fluent2.pyramid.net \
    --cc=glynn.clements@virgin.net \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=mailing-lists@xs4all.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).