Cryptic C code?

Jonathan E. Quist jeq at laidbak.UUCP
Thu Aug 15 12:08:00 AEST 1985


>Relevant code: (Kernighan & Ritchie, chapter 5, page 105)
>
>     strcpy(s, t)  /* copy t to s; pointer version 3 */
>     char *s, *t;
>     {
> 	 while (*s++ = *t++)
> 	    ;
>     }
>  
>
>Bob Crane (tektools!bobc) writes:
>>    ... [text from K&R, everyone owns it, no point quoting again] ...
>> 
>> Yeaacch!!!!!!  It was still very cryptic to me the tenth time that I read
>> it!!!  A friend explained it to me by saying that the character in the
>> 'while' expression is converted to an int and that the NULL character has
>> an ascii value of 0 so the test will exit when the NULL character is
>> encountered.
>> 
>> I have trouble believing that the above has advantages of great
>> speed OR readability over:
>> 
>>    strcpy(s,t)  /* copy t to s; pointer version 2 */
>>    char *s, *t;
>>    {
>>       while ((*s++ = *t++) != '\0')
>> 	 ;
>>    }
>> 
>> Does anyone out there support the author by saying that Version 3 of
>> 'strcpy' is better than Version 2?

Personally, I find Version 3 easier to deal with, but then,
I learned C after years of assembly language bit-twiddling.
Version 3 (and many other "standard" C constructs) happens
to be the form I settled on in various assembly language
implementations with various microprocessors.  In my case,
compactness of code and speed were of utmost importance.
(In some cases, saving 10 bytes of instructions meant
the difference between using a 2K or 4K EPROM.)
In working on things like tty drivers and such,
I found forms without the "extra" comparison easier to live
with while scanning unfamiliar code, because, though cryptic,
it is compact and unambigous, and after a while (i.e. with experience),
I think is is easier to recognize (*s++ = *t++) than to
see ((*s++ = *t++) != '\0') and stop to think `what are they
comparing?'
I think it's more or less the same as finding it
easier to scan through lower case comments (as opposed to
all upper case) to find something I wrote "some time back."
I suspect that this has something to do with the fact that
there is more size contrast between lower case letters than
the same in upper case.  This is only my personal theory.

I don't mean to flame those who prefer version 3, this
is just my own preference.

As to efficiency, that would depend upon the hardware and
the cleverness of the compiler.  If the machine sets
a zero flag when a '\0' is transferred, then a
"branch if zero" type of intruction can be immediately executed,
without additionally comparing the character to 0.
Whether the compiler takes advantage of this is another matter...

Jonathan E. Quist
Lachman Associates, Inc.
ihnp4!laidbak!jeq
``I deny this is a disclaimer.''



More information about the Comp.lang.c mailing list