Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!gamma@EDN-UNIX.arpa From: gamma@EDN-UNIX.arpa (W. J. Showalter) Newsgroups: comp.lang.c Subject: Possible C Anomaly Message-ID: <9236@brl-adm.ARPA> Date: Fri, 11-Sep-87 10:17:39 EDT Article-I.D.: brl-adm.9236 Posted: Fri Sep 11 10:17:39 1987 Date-Received: Sun, 13-Sep-87 09:27:02 EDT Sender: news@brl-adm.ARPA Lines: 62 I would like to get some comments on the following problem involving what I consider an inconsistency in the C programming language. Consider the statment z = (a > b) ? a : b; which is taken directly from Kernighan and Ritchie on page 47. According to them the statement is equivalent to if (a > b) z = a; else z = b; The other day I wrote the following code: k = 10; . . . . /* 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. According to K&R, the above expression is equivalent to /* Example #2 */ if (k > 1) k = k--; else k = 1; Note: In this case the else is not needed but included for completeness. 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. In the second example k does change like one would expect. I generated assembler listings for the two examples and discovered that example 1 makes used of an interim register to house the value of k BEFORE decrementing. It eventually moves this value back to k, replacing the decremented value with the original. I would like to get some other opinions on this if anyone has the time or interest. Also, although it seems to me that there is a slight inconsistency within the language I tried 3 different compilers on 3 different machine architectures and they all handle it the same way. I assume that there must be some well known and accepted BNF which is used to arrive at this consistency between the different compilers. Jim gamma@edn-unix.arpa