samperi@marob.MASA.COM (Dominick Samperi) (09/13/88)
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 ; Apparently FORTRAN permits the compiler to evaluate the expression in any order it pleases, for optimization purposes. -- Dominick Samperi, NYC samperi@acf8.NYU.EDU samperi@marob.MASA.COM cmcl2!phri!marob uunet!hombre!samperi (^ ell)
gwyn@smoke.ARPA (Doug Gwyn ) (09/13/88)
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) Why burden the net with this when it is easy to find in K&R? The theoretically associative and commutative operators have always been allowed to be rearranged in C. Before ANSI C (which hasn't happened yet!), this was true even if the programmer tried to enforce a particular grouping with parentheses (unlike Fortran). The operands of || and && operators obviously cannot be evaluated in reverse order due to the very definition of the || and && operators.
scjones@sdrc.UUCP (Larry Jones) (09/14/88)
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 ; K&R has always allowed the compiler to evaluate in any order at all (even in the presence of parentheses). The only operators which are guaranteed to evaluate left-to-right are "&&", "||", and ",". Ansi now requires compilers to evaluate "as if" they honored parentheses, and perhaps even associativity in unparenthesized expressions (there's still some debate as to exactly what the wording says). ---- Larry Jones UUCP: uunet!sdrc!scjones SDRC scjones@sdrc.uucp 2000 Eastman Dr. BIX: ltl Milford, OH 45150 AT&T: (513) 576-2070 "Save the Quayles" - Mark Russell
francis@proxftl.UUCP (Francis H. Yu) (09/22/88)
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 ; "a && b" is a control structure which implies "if (a) if (b) ... " It has nothing to do with the order of evaluation of expression. "Arithmetic operators associate left to right" - pp 41, K&R C Second Edition
gwyn@smoke.ARPA (Doug Gwyn ) (09/22/88)
In article <804@proxftl.UUCP> francis@proxftl.UUCP (Francis H. Yu) writes: >"a && b" is a control structure which implies > "if (a) if (b) ... " >It has nothing to do with the order of evaluation of expression. That is quite wrong. "&&" is a binary operator", a && b" is an expression, and the subexpression "a" is required to be evaluated. Then, if the value of "a" is nonzero, the expression "b" is required to be evaluated; otherwise "b" is required to NOT be evaluated. The following C statements are legal and useful: c = a && b; d = e + (a && b);
jones@ingr.UUCP (Mark Jones) (09/22/88)
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.