printf extensions for output of arrays

J

jacob navia

Has anyone here any information about how arrays can be
formatted with printf?

I mean something besides the usual formatting of each element
in a loop. I remember that Trio printf had some extensions but
I do not recall exactly if they were meant for arrays.

Thanks
 
K

kuyper

jacob said:
Has anyone here any information about how arrays can be
formatted with printf?

I mean something besides the usual formatting of each element
in a loop. I remember that Trio printf had some extensions but
I do not recall exactly if they were meant for arrays.

Non-standard extension to C are not, in themselves, on-topic for
comp.std.c. You could bring it on-topic by suggesting that some such
extension be standardized, or by asking whether a conforming
implementation is allowed to provide such extensions, or any of a
number of other variations. As it stands, your inquiry should have been
posted only to comp.lang.c.
 
W

Walter Roberson

Non-standard extension to C are not, in themselves, on-topic for
comp.std.c. You could bring it on-topic by suggesting that some such
extension be standardized, or by asking whether a conforming
implementation is allowed to provide such extensions, or any of a
number of other variations. As it stands, your inquiry should have been
posted only to comp.lang.c.

Over here in comp.lang.c we would have just said that such extensions
are not part of the C standard and so are not on topic here...
 
K

Kenny McCormack

Non-standard extension to C are not, in themselves, on-topic for
comp.std.c. You could bring it on-topic by suggesting that some such
extension be standardized, or by asking whether a conforming
implementation is allowed to provide such extensions, or any of a
number of other variations. As it stands, your inquiry should have been
posted only to comp.lang.c.

Funny on so many levels.
You have a career in comedy, to be sure.
 
M

Malcolm

Non-standard extension to C are not, in themselves, on-topic for
comp.std.c. You could bring it on-topic by suggesting that some such
extension be standardized, or by asking whether a conforming
implementation is allowed to provide such extensions, or any of a
number of other variations. As it stands, your inquiry should have been
posted only to comp.lang.c.
A question about whether it is possible to pass an array to printf() (as
some languages allow for their analgous output statements) would be topical.
However Jacob Navia knows C well enough not to need to ask this.
The question is therefore a proposal for an extension to the language, and
thus topical on comp.std.c, but not on comp.lang.c.
 
K

Keith Thompson

Malcolm said:
A question about whether it is possible to pass an array to printf()
(as some languages allow for their analgous output statements) would
be topical. However Jacob Navia knows C well enough not to need to
ask this. The question is therefore a proposal for an extension to
the language, and thus topical on comp.std.c, but not on
comp.lang.c.

That's assuming that jacob was actually proposing an extension to the
language. That's the most reasonable interpretation, but he didn't
actually say so, and I'm hesitant to make any assumptions.

jacob, can you clarify your question? Are you asking about the
language as it's currently defined (topical for comp.lang.c, but the
answer is simply "no"), about non-standard extensions to the language
(off-topic), or about a proposed enhancement for the next version of
the standard (topical in comp.std.c)?
 
J

jacob navia

Keith said:
That's assuming that jacob was actually proposing an extension to the
language. That's the most reasonable interpretation, but he didn't
actually say so, and I'm hesitant to make any assumptions.

jacob, can you clarify your question? Are you asking about the
language as it's currently defined (topical for comp.lang.c, but the
answer is simply "no"), about non-standard extensions to the language
(off-topic), or about a proposed enhancement for the next version of
the standard (topical in comp.std.c)?

As I said in comp.lang.c, I have a customer that wants that stuff.
I supposed that it is a common extension to printf and I just wanted
to know if other compilers have done this to be compatible with
them.

I will probably do as gcc, that implements a printf register
functionality :
http://docs.biostat.wustl.edu/cgi-bin/info2html?(libc.info.gz)Printf%20Extension%20Example

but it is not specific to arrays, what could be better, I do not know.

It *could* be a useful extension to the printf function probably, but I
do not want to add yet another sin to my already long list of extensions
requests :)
 
K

Keith Thompson

jacob navia said:
Keith Thompson wrote: [...]
jacob navia wrote:
Has anyone here any information about how arrays can be
formatted with printf?

I mean something besides the usual formatting of each element
in a loop. I remember that Trio printf had some extensions but
I do not recall exactly if they were meant for arrays.
[snip]
That's assuming that jacob was actually proposing an extension to the
language. That's the most reasonable interpretation, but he didn't
actually say so, and I'm hesitant to make any assumptions.
jacob, can you clarify your question? Are you asking about the
language as it's currently defined (topical for comp.lang.c, but the
answer is simply "no"), about non-standard extensions to the language
(off-topic), or about a proposed enhancement for the next version of
the standard (topical in comp.std.c)?

As I said in comp.lang.c, I have a customer that wants that stuff.
I supposed that it is a common extension to printf and I just wanted
to know if other compilers have done this to be compatible with
them.

I will probably do as gcc, that implements a printf register
functionality :
http://docs.biostat.wustl.edu/cgi-bin/info2html?(libc.info.gz)Printf%20Extension%20Example

but it is not specific to arrays, what could be better, I do not know.

It *could* be a useful extension to the printf function probably, but I
do not want to add yet another sin to my already long list of extensions
requests :)

Ok, that's much clearer.

Topicality in this case is a tricky question. The C standard
specifically allows extensions, so extensions aren't *inherently*
off-topic. I suppose I'd argue that a general question about what
extensions are out there is topical (more in comp.lang.c than in
comp.std.c), but detailed discussions of a particular implementation
should be taken to a system-specific newsgroup.

That's just my opinion; others are likely to differ.
 
D

Douglas A. Gwyn

jacob said:
Has anyone here any information about how arrays can be
formatted with printf?
I mean something besides the usual formatting of each element
in a loop. I remember that Trio printf had some extensions but
I do not recall exactly if they were meant for arrays.

It's not wise to extend print formatting, because code that
uses the extension won't be portable to other platforms.
Why in the world do you think printf should be looping
instead of the app that knows about the data structure?
 
R

Robert Gamble

Douglas said:
It's not wise to extend print formatting, because code that
uses the extension won't be portable to other platforms.

I think he was asking because he was considering adding it to his
implementation and wanted to know how others might have accomplished
this before, perhaps to avoid being incompatible with them, but I agree
with your premise.
Why in the world do you think printf should be looping
instead of the app that knows about the data structure?

Presumably to remove the overhead of calling printf many times. The
fact that printf already does this for arrays of characters via the 's'
conversion specifier make it logical for one to consider the
possibility of extending the functionality to other types. The
immediate difference one would notice is the fact that you would need
to know the exact number of items in the array since there is no analog
to strings for other types. The main issue I see however is telling
printf *how* to print an array of, for instance, integers. Do you
print the integers seperated by a single space, do you try and specify
the delimiters dynamically? How would you do this? It quickly becomes
obvious, at least to myself, that trying to provide a generalized
solution for this issue in the Standard library isn't worth much
thought, especially since it is easy enough to write a wrapper function
using vsprintf to better handle specialized needs.

Robert Gamble
 
K

kuyper

Douglas said:
It's not wise to extend print formatting, because code that
uses the extension won't be portable to other platforms.
Why in the world do you think printf should be looping
instead of the app that knows about the data structure?

The conceptual issue you raise doesn't seem especially
language-specific. Several other languages I'm familiar with such as
APL, IDL, and perl all handle arrays much more conveniently than C's
printf() does. Even Fortran print formats are capable of handling
arrays; it's one of the few ways in which Fortran printing is more
convenient than C printing. Do you think the array-printing facilities
of those other languages represent bad design for that same reason?
 
D

David R Tribble

Robert said:
I think he was asking because he was considering adding it to his
implementation and wanted to know how others might have accomplished
this before, perhaps to avoid being incompatible with them, but I agree
with your premise.

It would be interesting to see if the symtax of the format specifier
string was a truly upward compatible extension to ISO C,
especially in light of some of the additions in C99.
Presumably to remove the overhead of calling printf many times. The
fact that printf already does this for arrays of characters via the 's'
conversion specifier make it logical for one to consider the
possibility of extending the functionality to other types. The
immediate difference one would notice is the fact that you would need
to know the exact number of items in the array since there is no analog
to strings for other types. The main issue I see however is telling
printf *how* to print an array of, for instance, integers. Do you
print the integers seperated by a single space, do you try and specify
the delimiters dynamically? How would you do this? It quickly becomes
obvious, at least to myself, that trying to provide a generalized
solution for this issue in the Standard library isn't worth much
thought, especially since it is easy enough to write a wrapper function
using vsprintf to better handle specialized needs.

My thoughts exactly. Provide an 'array_printf()' that takes all this
information as extra parameters. The overhead of calling printf
multiple times pales in comparison to the amount of overhead
required by the actual I/O (write) operations.
 
J

jacob navia

The conceptual issue you raise doesn't seem especially
language-specific. Several other languages I'm familiar with such as
APL,

WOW!!!!

Another APL guy!

I remember Quad FMT. What a WONDERFUL printing function.

IDL, and perl all handle arrays much more conveniently than C's
printf() does. Even Fortran print formats are capable of handling
arrays; it's one of the few ways in which Fortran printing is more
convenient than C printing. Do you think the array-printing facilities
of those other languages represent bad design for that same reason?

C and arrays doesn't mix very well. I never understood why.

jacob
 
D

Douglas A. Gwyn

... Even Fortran print formats are capable of handling
arrays; it's one of the few ways in which Fortran printing is more
convenient than C printing. Do you think the array-printing facilities
of those other languages represent bad design for that same reason?

It's convenient *if* you can live with the formatting details
imbedded in the PRINT feature. FORTRAN, for quite some time
at least, didn't have good support for other kinds of data
structures and thus singling out the one kind that it did
support was fairly natural. Also note that it didn't support
creation of output lines piece by piece, so the user would
really be stuck for printing matrices without built-in
support. There aren't enough similarities between the
languages to use that as a guide.
 
R

Robert Gamble

David said:
It would be interesting to see if the symtax of the format specifier
string was a truly upward compatible extension to ISO C,
especially in light of some of the additions in C99.


My thoughts exactly. Provide an 'array_printf()' that takes all this
information as extra parameters. The overhead of calling printf
multiple times pales in comparison to the amount of overhead
required by the actual I/O (write) operations.

Perhaps. On top of the overhead of calling the function though there
is also the time it takes to parse the format string and the fact that
in certain circumstances multiple "little" writes to a stream may take
considerably longer than a single "larger" write. If you are printing
to a character buffer the time to perform the write will probably be
much smaller than writing to an output stream making the proportion of
time spent elsewhere more significant.

Robert Gamble
 
R

Robert Gamble

jacob said:
Has anyone here any information about how arrays can be
formatted with printf?

I mean something besides the usual formatting of each element
in a loop. I remember that Trio printf had some extensions but
I do not recall exactly if they were meant for arrays.

The Trio had a lot of interesting extensions via its trio_printf family
of functions, you are probably referring to the trio_printfv (the v for
vector I suppose) function which takes an array of pointers to void as
its second argument. For example, "trio_printfv("%d %d %f\n", vp);"
would expect vp to be the address of the first void pointer in an array
of 3 void pointers that could be converted to pointers to int, int, and
double respectively. This doesn't sound like it accomplishes what you
are looking for, I haven't ever seen anything that does.

Here is what I came up with (I haven't put much thought into this, it
should probably be considered a testament to why this isn't a good
idea):

Example 1:
my_printf("%[10]d\n", int_array);

This would print out 10 integers from int_array, I use the the brackets
to indicate an array is the argument and specify the number of items
from the array to print. In this example, the integers would be
seperated by a single space, this would be the default.

Example 2:
my_printf("%[10%c]d\n", int_array, ",");

In this example we explicitly specify the delimiter by using a second
conversion specifier inside the brackets after the number of array
members, in this case 'c' for character. The argument corresponding to
the delimiter follows the array argument.

Example 3:
my_printf("%[10%s]d\n", int_array, "my delimiter string");

Same as above but specify a string as the delimiter.

Of course you can use field width, precision, etc. with this format:
my_printf("%6.2[10]f\n", double_array);

If this is not enough control over delimiter choices, you can specify a
list of delimiters as well:

Example 4:
my_printf("%[10%[5]c]d\n", (int []){1,2,3,4,5,6,7,8,9,10}, "abcde");

Specifies that we want to print an array of 10 integers delimited by an
array of 5 characters and that we want to iterate through the
characters as delimeters wrapping back to the first one after the last
one is used.

This would of course print:
1a2b3c4d5e6a7b8c9d10

Notice that there is no delimiter following the last member printed.

We won't allow any further nesting of conversion specifiers for
simplicity sake but we should allow multiple non-nesting conversion
specifiers in the delimeter string:
my_printf("%[3%c%[2]c]d\n", (int []){1,2,3}, "-", "*/");
would print:
1-*2-/3

Of course, you could also provide a pointer to a function that is
called after printing each value to return the delimiter for that
value, I'll spare you the semantics of that one.

I'd be interested to know what you come up with.

Robert Gamble
 
D

Douglas A. Gwyn

jacob said:
C and arrays doesn't mix very well. I never understood why.

Basically it's because in C pointer (to whatever type) is a
more fundamental concept, and an array name is converted to
pointer-to-first-element (thus losing the length information)
in most contexts. Arrays are *not* first-class objects in C,
as witness what happens when you try to assign one array to
another.

As of C99, there is a generally richer kind of array type,
under the name VLA (Variable-Length Array).
 
S

Suman

Robert said:
[replying to jacob navia]:
Has anyone here any information about how arrays can be
formatted with printf?
[...]

Example 2:
my_printf("%[10%c]d\n", int_array, ",");
--^
In this example we explicitly specify the delimiter by using a second
conversion specifier inside the brackets after the number of array
members, in this case 'c' for character. The argument corresponding to
the delimiter follows the array argument.

Are you sure you meant a "," and not a ','?
Example 3:
my_printf("%[10%s]d\n", int_array, "my delimiter string");

Same as above but specify a string as the delimiter.

So much to support me.

Or did I, in some way fail to make sense of your post. It made for
a nice thought provoking read, though. Thanks.
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top