tags:

views:

131

answers:

6

i have multiple outputs that i need to print as columns side by side. is there any way to achieve this using C? something like the output of ls -a i.e. i want to print the first column data, then the second and so on

+5  A: 

You can do this with format strings.
See the printf(3) manpage for more info.

Steve Lazaridis
What an informative answer! I suppose you've been working on that all evening.
Chris Lutz
Haha. Yes, I know it's short, but explaining printf is way better accomplished by the manpage than by anything I could write up here. Besides, reading that manpage is a huge benefit to beginners.
Steve Lazaridis
The least you can do is provide a link to a nice manpage (I go with opengroup myself) and/or a simple code snippet that demonstrates the concept in question. Reading documentation is good, but the point of SO is to be a place with answers to questions, not a place with links to answers to questions.
Chris Lutz
A: 

using "\t" and "\n"

Example:

for(int i = 0 ; i < m ; ++i)
{
  for(int j = 0; j < n ; ++j
 {
    printf("%d\t", a[i][j]);
 }
 printf("\n");
}
aJ
Tabs are a rather primitive and inaccurate way to control columns.
Chris Lutz
+5  A: 

You'll want to specify field widths, this question has a pretty good example of what you're looking for.

Mark E
+2  A: 

If you mean automatically calculating the number of columns and their widths and so on, you can't do this with printf() alone. However, it's possible to add extra code to precompute the number and width of columns. For instance, consider the OpenBSD code for ls.

mrkj
+1 for the suggestion but I think it is a bad example as it ignores multi-byte encodings as well as 0-2 width glyphs. Better getting that right on new code.
jbcreix
You make a good point, though it's tempting to point out that the question specifically asked for output like `ls -a`... where better to look than the code for `ls`?
mrkj
+2  A: 

EDIT (changing my whole answer):

For the formats that you get for free with printf (including some to do with length): http://www.cplusplus.com/reference/clibrary/cstdio/printf/

I would avoid using tabs to do this. Tabs can be interpretted in different ways on different machines (2 spaces, 4 spaces, n spaces). If this is a fun app just for you on your machine, tab away...but the following example is just as easy.

RIGHT JUSTIFY (8 is the width, s means string, | is just to show spacing):

printf("|%8s|%8s|","try","this");

would result in this:

|     try|    this|

LEFT JUSTIFY (- means left-justify, 8 is the width, s means string, > is just to show spacing):

printf("%-8s%-8s>","try","this");

would result in this:

try     this    >
santosc
A: 

Let me make sure I understand; you have some code like

while (some_condition)
{
  bread();
  bwrite();
  pgsize();
}

and you want to display the outputs of those functions as

bread    bwrite    pgsize
1234     5678      1024
2345     6789      1024

Is that close to what you meant?

If so, then you have a couple of choices. First, each function prints its output, but without a newline:

void bread()
{
  ...
  printf("%*.*d", field_width, precision, value);
  fflush(stdout)
  ...
}

where field_width is the minimum width of the column, precision indicates the minimum number of digits to be printed, and value is whatever your function is printing. The * in the conversion specifier allows you to pass the width and precision as arguments to printf(), i.e.,

printf("%*.*f", 10, 2, 3.14159);

is the same as

printf("%10.2f", 3.14159);

The chief advantage of using the * flags in the conversion specifier means you can adjust your output format at runtime. Note that * in a conversion specifier means something completely different in scanf().

Either the last function called or the caller will need to write a newline to the output stream.

Alternately, you can have each function return its value to the caller, and let the caller do all the output:

while (some_condition)
{
  int r = bread();
  int w = bwrite();
  int s = pgsize();
  printf("%*.*d%*.*d%*.*d\n", rwidth, rprec, r, wwidth, wprec, w, swidth, sprec, s);
}

where rwidth, rprec, etc., have all been declared and set elsewhere.

John Bode