How to print 80 col name-pairs?

B

beagle197

Folks,

What is a good way to print a standard 80 (or less) column of text
where the left side being a field name text, and right side being the
space padded value? e.g. "image_handle<space-padding>0xff". This is
useful for outputting many lines of aligned debugging data, making it
easier to read and track down bugs.

I have something like this:

/* gcc -W -Wall -o handle handle.c */
#include <stdio.h>

int main(void) {
int nArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int *handle = &nArray[0];

fprintf(stderr, " int_ptr 0x%x\n", (unsigned int) handle);
return 0;
}

Outputs :

int_ptr 0xbffffa70

Seems I could use some sprintf macros that computes the length, and
offsets with the approp number of spaces between the field value
pairs, but I wondered if anyone has suggestions.

Thanks,
BEA
 
R

Richard Heathfield

(e-mail address removed) said:
Folks,

What is a good way to print a standard 80 (or less) column of text
where the left side being a field name text, and right side being the
space padded value?

If I'm reading you correctly:

printf("%30s%0x\n", "int_ptr", (unsigned int)handle);

will display int_ptr in a 30-character-wide field. It will be
right-justified, but using %-30s instead will left-justify it nicely for
you.
 
C

CBFalconer

Nice move, Richard, this is exactly what was needed.

Note where Richard placed his answer, and what he snipped.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. I fixed this one. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top