parsing the format string at compile time...

Michael Meissner meissner at tiktok.dg.com
Mon Oct 16 04:06:49 AEST 1989


In article <6737 at hubcap.clemson.edu> drcook at hubcap.clemson.edu (david richard cook) writes:
| From article <705 at nixbur.UUCP>, by jobrien at nixbur.UUCP (John O'Brien):
| > printf call is a constant (which it is most of the time), the compiler
| > should be able to parse the string at compile time, and turn the printf
| > call into something like the series of Modula-2 calls, with a correspond-
| > ing increase in efficiency.  Do C compilers do this?  Are there any prob-
| > lems with doing this?
|
| 	If C did allow this, it would not be C.  The compiler knows
| nothing about any functions, including I/O.

Wrong.  The ANSI standard gives explicit license for implementations
to 'know' about any of the standard functions in section 4.  Nothing
requires that a user call to 'printf' call an actual routine printf.
All that is required, besides getting the corect result, is that an
implementation not evaluate arguments more than once (except for
grandfathering putc/getc/putchar/getchar, and possibly a few others
that have historically been macros), and that taking the address of
the function work.  Granted I don't know about any compilers that
currently do this with printf, but there are compilers which will do
special things for certain library routines (typically math or
memory/string copies and compares).  For example, the Data General MV
C compiler has quite a few routines that are builtin, such as memcpy,
strcpy, memcmp, strcmp, abs, fabs, sin, etc.  Also, the GNU C compiler
has support for builtin functions, though at present there aren't that
many (abs, labs, fabs, alloca, etc.).

|					       Some new compilers may be
| able to this, though I do not know of any, by using the #pragma
| preprocesser directive to declare certain functions as builtin.  The
| use of #pragma is left up to the compiler implementation and not C.
| If an ANSI C implementation does not understand what the #pragma
| directive is trying to accomplish, it will simply ignore it.

Even though I originally voted for pragma in the ANSI committee, I
feel that the current semantics are a botch....

Michael Meissner, Data General.				If compiles where much
Uucp:		...!mcnc!rti!xyzzy!meissner		faster, when would we
Internet:	meissner at dg-rtp.DG.COM			have time for netnews?



More information about the Comp.lang.c mailing list