Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!mrd@sun.mcs.clarkson.EDU From: mrd@sun.mcs.clarkson.EDU (Michael R. DeCorte) Newsgroups: comp.lang.c Subject: Possible C Anomaly Message-ID: <9270@brl-adm.ARPA> Date: Mon, 14-Sep-87 11:56:12 EDT Article-I.D.: brl-adm.9270 Posted: Mon Sep 14 11:56:12 1987 Date-Received: Tue, 15-Sep-87 05:41:57 EDT Sender: news@brl-adm.ARPA Lines: 35 . /* Example #1 */ k = (k > 1) ? k-- : 1; My intent was to decrement k with each pass until it reached 1 and then to keep it at one. There are a variety of ways to accomplish this but I just happened to try this one first. The problem is is that the two examples behave differently. In the first example k does not change values. It remains equal to 10 indefinitely. Try k = (k > 1) ? --k : 1; or k = (k > 1) ? (k - 1) : 1; from page 42 of KR "But the expression ++n increments n before using its value, while n++ increments n after its values has been used." You will also notice the chart on page 49 in section 2.12 Precedence and Order of Evaluation for the expression k = (k > 1) ? k-- : 1; 1 2 3 the compiler evaluates the k(3) yielding an rvalue. It then decrements the lvalue k. It then assign to the lvalue k(1) the rvalue generated by k(3). As k(3) was equal to k you have an infinite loop. This what is supposed to happen. Michael DeCorte mrd@clutx.clarkson.edu