On Fri, Oct 26, 2001 at 09:40:38AM -0500, Mark Skeels wrote: > Hi, List. > > Out ofnecessity, I continue to study C. > > On page 105 of "The C Programming Language", there are two versons of > function strcpy: > > version 1: > void strcpy(char *s, char *t) > { > while ((*s = *t) != '\0') { > s++; > t++; > } > } > > version 2: > void strcpy(char *s, char *t) > { > while ((*s++ = *t++) != '\0') { > ; > } > } > > Before the second version, we read the following statements... > > "Experienced C programmers would prefer..." > > My question is, could you explain why it is good to move both the asignment > operation and the increment operation into the while test portion? It's more compact. C was designed in an era when processing power was limited, memories were small, and optimizing was still in its infancy. A non-optimizing compiler would generate more code for the first than the second, making it bigger and slower. C programmers of the '70 were speed freaks. Truth be told an experienced C programmer would have written it as: void strcpy(char *s, char *t) { while (*s++ = *t++); } simply omitting the compare to zero because the loop would complete after the terminating NULL was copied. All of this is superfluous in more of today's compilers. Almost every one would optimize the code into the third example. > > As a Pascal programmer, I was taught that anything that makes the program > more readable is good. This does not seem to qualify on that count. What's > more, it seems to muddle the entire while loop by mixing assignment and > increment with the test portion. This isn't Pascal. C's primary tenent has always been that the programmer knows what they are doing. So it sprinkles lots of different optimizing techniques into the language for folks to take advantage of. This cultural trait persists because there's a hoard of legacy programs and legacy programmers who continue to generate tight, yet cryptic code. But again the point is moot. Feel free to code in whatever manner you feel comfortable. But be aware that you'll need to understand all of the tricks in order to understand a large body of the code that's out there. > > Also, what is the logic behind understanding the actual test in the while > loop? I can't understand from the syntax exactly what is being compared to > '\0'. Is it *s? *t? If so, why? Assignment is an operator in C, not a statement. The value of an assignment is the value that is assigned. For example: i = 3; Places the value 3 into i, it also produces a value of 3 as the result. So code like: j = 2 + (i = 3); Would assign i with 3, and then add the result (3) to 2 and assign that to j. So to answer your question, the value of *t is the result of the assignment. Also note that t moves from one space to the next. Since the increment is after the t, it occurs after the value of *t is obtained. > > Comments? There are different ways of doing it. Do what you feel comfortable with, as most modern optimizing compilers will take care of generating efficient code. It's just different cultures. BAJ -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.