Hi, theoretically, the advantage of the 2nd version is, that the 1st one implements a comparison against a constant (which could be an arbitrary value), and the latter one checks whether the value of the pointed area zero. Most architectures does the 2nd task more simple than the 1st one. On the other hand, the compiler may be able to recognize that the constant mentioned above is zero and *could* optimize the generated code to achieve the tighter version would produce the 2nd code. But it is not necessary the case in all situations. Regards, Imre +-----------------------------------------------------------------------+ | The information transmitted is intended only for the person or entity | | to which it is addressed and may contain confidential and/or | | privileged material. Any review, retransmission, dissemination or | | other use of, or taking of any action in reliance upon, this | | information by persons or entities other than the intended recipient | | is prohibited. If you received this in error, please contact the | | sender and delete the material from any computer. | +-----------------------------------------------------------------------+ On Fri, 26 Oct 2001, Dale Botkin wrote: > > Out of necessity, I continue to study C. > > Yeah, me too. > > > 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? > > > 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's testing (*s++ = *t++) compared to '\0'. In effect, it copies *t to > *s then compares to 0, then increments. But why really would > "experienced" C programmers like the second form better? Well, less > typing... that's a big plus... they do the exact same thing, and CCS > compiles both to the same size. > > Dale > > -- > http://www.piclist.com hint: The PICList is archived three different > ways. See http://www.piclist.com/#archives for details. > > > > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body