Various messages compined into one - quotes are not all from the
same person(s).
Straighten up and get serious. You used twice as much
printf as
everybody else. Have you ever seen the setup for a printf call in
assembler?
I wonder if even the compiler can save you from this shameless bit of excess.
Unlikely - as there are two different format strings.
... the wrong answer being to dump out each array
element followed by a
comma, then output ^H as the final step in the function ;-)
Works poorly on hardcopy devices :-)
Real Programmers would presumably use putc exclusively
in favour of the
more computationally expensive printf...
Naw, printf() is a simple and versatile tool, and since the code is only
linked once it's usually worth it as it's handy "all over" - besides,
printf() doesn't have to be huge - here is my CSTATS output for my integer-
only printf format routine (which is a single function that does not call
ANY other functions - all tests and conversions are internal):
Characters:
in file(s) : 2263
in comments : 733
whitespace : 620
significant : 910
Lines:
in file(s) : 97
blank/comment: 23
significant : 74
Cism's:
'{'s : 13
'}'s : 13
';'s : 47
comments : 20
74 lines and 47 statements (including variable declarations) in 13 blocks.
This is an integer-only printf() formatter (real programmers don't use FP)
written in pure C which supports the following free form types:
%c (character)
%s (string)
%d (signed decimal)
%u (unsigned decimal)
%x (hexidecimal)
%o (octal)
%b (binary)
%% (single '%')
And formatting of any of the above types in the form:
%5x <= 5 character, right justify, space fill
%05x <= 5 character, right justify, zero fill
%-5x <= 5 character, left justify, space fill
%-05x <= 5 character, left justify, zero fill (kinds useless :-)
[and yes, other format widths besides 5 are supported!]
- Codesize compiled from C for the 8086 (my Micro-C) is 688 bytes.
- Code size of comparable routine hand crafted in assembly language
for my 8051 compiler library is 437 bytes (although this adds a
new %i for strings in internal memory).
- Code size for the C-FLEA (a virtual processor I developed which is
an optimized C target) is 335 bytes.
Just because winbloat leads us to expect multi-megabyte executables and
massive libraries - it duzn't has to be so!
void print_ary
(int *aryp, int n)
{
int i;
for (i=0; i<n; aryp++, i++)
{
if (i>0)
printf (", ");
printf ("%u", *aryp);
}
printf ("\n");
}
Um, if you're anal like me, you don't want to print a comma after the last
value, so:
...
printf ("%u", *aryp);
if (i<n) printf (", ");
Um, the original code won't print a comma after the last value.
You could of course do something "clever" to make the
extra conditional harder to see like:
for(i=0; i < n; ++i)
printf("%u%s", aryp[i], ","+(i >= n));
This is getting silly!
Regards,
--
dave04a (at) Dave Dunfield
dunfield (dot) Firmware development services & tools:
www.dunfield.com
com Collector of vintage computing equipment:
http://www.parse.com/~ddunfield/museum/index.html