----- Original Message ----- From: "Mark Skeels" To: Sent: Friday, October 26, 2001 7:40 AM Subject: [PIC]: or [EE]: C syntax question > 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? > > 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. In the good old days the compiler would likely be able to make better code out of the second version than the first. Modern compilers should be able to do about the same with either. I agree that obscuring the code by jamming everything into the while condition is bad, but sometimes it can make sense: while ( 0 != (bytes_read = read()) ) { ...do something with the bytes... } The typical alternative would be: bytes_read = read(); while (0 != bytes_read) { ...do something with the bytes... bytes_read = read(); } This of course has its own problems. > 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? It is the value assigned to *s. This is becauase the value of an assignment statement is the value of the left hand argument. Bob Ammerman RAm Systems -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.