Problem with #define'd macro...

Vox Populi ravim at gtenmc.UUCP
Fri Mar 29 11:55:30 AEST 1991


In article <1991Mar20.150301.9941 at evax.arl.utexas.edu> c145gmk at utarlg.uta.edu writes:
>Attempting to compile the following (fragment, but displays the problem)
>results in an 'invalid target for assignment' error on our VAX running

>
>#define DIV_MOD(d,r,x,y) ( y == 0.0 ? d = 0.0, r = x : d = x/y, r = x - d*y )

>a = 5, b = 2; DIV_MOD(quotient,remainder,a,b);

I think the compiler error that you were getting is because, operator
precedence is not observed here correctly.

According to K&R precedence table, operator ',' has lower precedence (in fact,
least precedence) than the tertiary operator '? :'. 
Hence you would need to raise the precedence of ',' operators in the
operands of '? :' in the macro defined above.

Hence, the correct definition would be
#define DIV_MOD(d,r,x,y) ( y == 0.0 ? (d = 0.0, r = x) : (d = x/y, r = x - d*y))

	-	Ravi Mandava

-- 
**********************   #include <stddisclaimer.h>  **************************
Ravi Mandava			e-mail :	ravim at gtenmc.gtetele.com
					  or    ravim at gtenmc.UUCP
*******************************************************************************



More information about the Comp.lang.c mailing list