Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!cs.utexas.edu!swrinde!ucsd!ogccse!blake!uw-beaver!mit-eddie!attctc!bobc From: bobc@attctc.Dallas.TX.US (Bob Calbridge) Newsgroups: comp.lang.c Subject: Re: evaluation order Summary: because Message-ID: <9366@attctc.Dallas.TX.US> Date: 15 Sep 89 00:40:43 GMT References: <9361@attctc.Dallas.TX.US> Distribution: na Organization: The Unix(R) Connection, Dallas, Texas Lines: 64 In article , jdr+@andrew.cmu.edu (Jeff Rosenfeld) writes: > > Excerpts from netnews.comp.lang.c: 14-Sep-89 evaluation order Bob > > Calbridge@attctc.Dal (1134) > > > By way of example, if I wanted to avoid the replication of > > of strlen() in the following example: > > > if (write(handle, buf, strlen(buf)) != strlen(buf)) do_something(); > > > by using rephrasing it like: > > > if (write(handle, buf, len=strlen(buf)) != len) do_something(); > > > What's wrong with > len = strlen(buf); > if (write(handle,buf,len) != len) do_something(); > ? > > Just because C lets you do funky things doesn't mean that you have to do > them all the time. If you really want to do it in one statement you can: > > len=strlen(buf), write(handle,buf,len) != len ? do_something() : 0 ; > > But hopefully you'd rather not. > - Jeff. As I said, this was by way of example. I reduced it to a simpler form of the real structure I wanted to consider. To wit while (((ch=getch()) != CR) && (ch != LF)) { doitoit() doitagain(); doitlasttime(); } I did a simple routine that I use often called ynget (int row, int column, char *prompt) that basically looked like this: gotoxy (row, column); puts (prompt); do { ch = toupper(getch()); } while ((ch != 'Y' && (ch != 'N')); return (ch == 'Y'); where I figured that it would have been simpler to do something like while (((ch = toupper(getch())) != 'Y') && (ch !='N')); Well, not necessarily simpler, but more compact. I don't know if this would necessarily result in shorter object code or faster but it wouldn't matter if I couldn't trust the evaluation order. Having read that && is a short circuit operator this may not be the precise example to make clear what I'm looking at but I think it gets the major point across. -- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- = I know it's petty.......... = - But I have to justify my salary! - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=