Liste des Groupes | Revenir à cl c |
bart <bc@freeuk.com> writes:
This is a C program using one of the extensions from my old
compiler:
>
#include <stdio.h>
#include <time.h>
>
int main(void) {
time_t t = clock();
printf("%v\n", t); # (used to be '?'; changed to
'v') }
>
The compiler replaces the 'v' format with a conventional format code
according to the type of the expression. For my 'time_t', it happens
to be 'lld'.
That's nice. Seriously, it's nice. If it were added to a future
edition of the language, I'd likely use it (once I could count on it
being supported, which would take a while).
The Go language has something like that.
You can add extensions like that to your own compiler easily
enough. Adding them to the C standard (which requires getting all
implementers to support them) is a lot harder. Does it work for
both output (printf) and input (scanf)?
What if the format string
isn't a string literal; does the compiler generate code to adjust
it, allocating space for the translated string and deallocating it
after the call? Does it work with printf("%n", &var)? What about
qualifiers, specifying width, base, and so forth. How do I print
an integer of arbitrary type in hexadecimal in a right-justified
8-digit zero-padded field? The feature implies an ability for
generic code that works with different types; can programmers use
that feature for things other than format strings? How much more
complicated would the C language (as distinct from the library)
have to be to support this?
If you have answers to all those questions, and to all the other
questions that haven't occurred to me, I wouldn't mind seeing
something like that in a future version of C. I haven't looked
closely at Go, but it's a compiled language with a feature similar
to what you describe; it could probably be mined for ideas.
Or maybe we should be thinking in terms of something other than format
strings. The idea that "%v", which is a 3-byte chunk of data, has
compile-time implications in certain contexts is a bit unnerving.
Ada chose the short name "Put" for its output routines.
It's overloaded, so you can write `Put(this); Put(that);
Put(the_other);` Maybe that a little too verbose, but how about
a new built-in operator that takes an argument of any of various
types and yields something that can be printed? '$' is available.
I haven't thought this through.
Les messages affichés proviennent d'usenet.