Xref: utzoo comp.lang.c:12861 comp.lang.fortran:1259 Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!cornell!batcomputer!itsgw!steinmetz!uunet!ingr!jones From: jones@ingr.UUCP (Mark Jones) Newsgroups: comp.lang.c,comp.lang.fortran Subject: Re: C associativity rules Keywords: associativity, expressions Message-ID: <2595@ingr.UUCP> Date: 22 Sep 88 14:45:52 GMT References: <412@marob.MASA.COM> <804@proxftl.UUCP> Organization: Intergraph Corp. Huntsville, Al Lines: 69 In article <804@proxftl.UUCP>, francis@proxftl.UUCP (Francis H. Yu) writes: > In article <412@marob.MASA.COM> samperi@marob.MASA.COM (Dominick Samperi) writes: > >Does K&R C or the new ANSI C permit the compiler to evaluate an expression > >like a+b+c in any order it pleases, rather than in the strict left-to-right > >order (a+b)+c ? I've always assumed that a strict left-to-right order would > >be used, as is the case for relational expressions like the one in the > >following. > > while(i != -1 && a[i] != k) > > whatever ; > Under ANSI, I believe that within a conditional, everything is evalutated left to right(with precedence inforced) such that if((ptr = get_struct()) && ptr->type == MYTYPE) whatever; and if(argv[0] && *argv[0] && **argv[0]) progname = argv[0]; will work correctly, ie if get_struct returns NULL, ptr->type will not be evaluated. or if argv[0], *argv[0] will not be evalutated. This means that you can test for certain things first. For instance: struct Event { int x,y; }event; if(event.x == 100 && event.y == 150) do_gadget_1(); else if(event.x == 100 && event.y == 250) do_gadget_2(); else if(event.x == 200 && event.y = 45) do_gadget_3(); . . . can be done as if(event.y == 150 && event.x == 100) do_gadget_1(); else if(event.y == 250 && event.x == 100) do_gadget_2(); else if(event.x == 200 && event.y ==45) do_gadget_3(); . . . The second case eliminates the need to check and see if event.x == 100 twice, it will only be tested when event.y has been matched. And this is without optimizations, or make the code too unreadable. Mark Jones PS yes I know there is a switch statement, I know how to use it, and this is only an example and demonstration. PPS if the above suppostion about ANSI is wrong, I would like to know. is this same thing true for K&R.