Path: utzoo!mnetor!uunet!husc6!yale!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: comma operator Message-ID: <10176@mimsy.UUCP> Date: 14 Jan 88 04:47:22 GMT References: <3819@sigi.Colorado.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 34 [More operator drivel :-) ] In article <3819@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes: >I'd never used the comma operator before (except in for() statements).... [five examples deleted] >All five gave the answer "1 0," and (using Turbo C) they all generate the >same amount of code (OBJ = 223, EXE = 5408). They should; semantically, they were all the same. >So, what is the advantage of using the comma operator other than >to squeeze everything on to one line? Not `one line', but rather `one expression', such as is required in `for' statements. Comma expressions can also be useful in translating a function to a macro: /* * Get the next nybble. This is an expression rendition of * if (--pk->pk_1nyb < 0) { * pk->pk_1nyb = 1; * pk->pk_c = nextbyte(pk); * return (pk->pk_c >> 4); * } else * return (pk->pk_c & 0xf); */ #define nextnyb(f) \ (--(pk)->pk_1nyb < 0 ? \ ((pk)->pk_1nyb = 1, ((pk)->pk_c = nextbyte(pk)) >> 4) : \ (pk)->pk_c & 0xf) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris