evaluation order

Bob Calbridge bobc at attctc.Dallas.TX.US
Fri Sep 15 10:40:43 AEST 1989


In article <UZ3ta3_00XcAI4hY08 at andrew.cmu.edu>, jdr+ at andrew.cmu.edu (Jeff Rosenfeld) writes:
> > Excerpts from netnews.comp.lang.c: 14-Sep-89 evaluation order Bob
> > Calbridge at 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!                           -
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



More information about the Comp.lang.c mailing list