Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcvax!ukc!icdoc!cl-jenny!am From: am@cl.cam.ac.uk (Alan Mycroft) Newsgroups: comp.lang.c Subject: Re: fabs(x) vs. (x) < 0 ? -(x) : (x) Message-ID: <665@jenny.cl.cam.ac.uk> Date: Tue, 3-Feb-87 07:01:08 EST Article-I.D.: jenny.665 Posted: Tue Feb 3 07:01:08 1987 Date-Received: Sat, 7-Feb-87 06:39:07 EST References: <4943@mimsy.UUCP> <2550005@hpisod2.HP> Reply-To: am@cl.cam.ac.uk (Alan Mycroft) Organization: U of Cambridge Comp Lab, UK Lines: 32 In article <2550005@hpisod2.HP> decot@hpisod2.HP (Dave Decot) writes: >> In article <4477@ut-ngp.UUCP> jjr@ngp.UUCP (Jeff Rodriguez) writes: >> >Why isn't fabs() implemented as a macro [ (X) < 0 ? -(X) : (X) ]? >> >You could implement fabs() as a macro as follows: > > #define fabs(X) ((_fabs = (X)), (_fabs < 0? -_fabs : _fabs)) > >if _fabs were declared as a float in the math library. > Of course you could (modulo volatile), but what this example (and many others like it) REALLY SHOWS, is that C suffers from the lack of the ability to make declarations within expressions. The pre-processor merely amplifies this deficiency -- as we would all like to be able to write macro's which evaluate each argument once (wouldn't we?). Many languages (including BCPL from which C derives) have this ability. E.g. (ML) ::= let in | ... (BCPL) ::= valof | ... ::= | resultis | ... The absence of this ability (in suitably C-like syntax) to create explicit temporaries in C expressions explains why getc() and putc() are allowed to evaluate their parameters more than once. Question: do not the ANSI committee find this uncomfortable too? Is the absence of suitable concrete syntax the justification? BTW, I have been involved in writing a C compiler in which this facility was abstract syntax (with no concrete correspondent) and which proved very useful for (almost-)source level sub-expression elimination and de-sugaring of bitfields etc.