On 05/01/12 11:43 PM, Fred Cisin wrote:
> My
canonical example,
> while (*T++=*S++);
> just for the sake of readability, is easier for a beginner OR STUDENT to
> follow as something like:
>
> do
> {
> X = *S;
> *T = X;
> S++;
> T++;
> }
> while('\0'<> X); /* Yes, of course, (X), but that's my point.
*/
On Thu, 5 Jan 2012, Toby Thain wrote:
ITYM !=
Probably. Certainly for my own use or second semester students.
With most C compilers either would work.<> is slightly more intuitive
I've never encountered a compiler which would accept <>; e.g. gcc does
not. Which one are you referring to? Microsoft's maybe?
for beginners than != until they recognize it as a
crude drawing of a
not-equal sign. Once they've got THAT, though, they're probably ready for
while(X)
One trivial mistake of style that I made in this example, was that because
I used 'S' for "source" and 'T' for "target", I
didn't have 'T' available
for the temporary variable name.
(Readability and ease of understanding is more important than efficiency
for EXPLAINING something)
The long version isn't less efficient.
I meant as a general principal.
BUT, even in this example, unless the compiler does "optimization" to
compile something other than what you asked for (DWIM!) and removes the
temporary variable, the long version will be a TINY bit slower due to the
extra temporary variable being written and read,...
Not at all. It's an obvious optimisation...
If the purpose is the fastest possible code, that stuff matters.
gcc version 4.0.1, -O3, produces *identical* assembly for both versions,
on my machine (and probably yours).
If the purpose is having the students understand it,
without having to
decipher the "puzzle code", delaying them moving on to the rest of the
topic, the long version is far superior.
Yes, probably. But I expect any C programmer to be able to reliably read
this idiom (we had that thread a few months ago).
--Toby
.section __TEXT,__text,regular,pure_instructions
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
.machine ppc
.text
.align 2
.p2align 4,,15
.globl _a
_a:
.p2align 4,,15
L3:
lbz r0,0(r4)
addi r4,r4,1
extsb r0,r0
cmpwi cr7,r0,0
stb r0,0(r3)
addi r3,r3,1
bne cr7,L3
blr
.align 2
.p2align 4,,15
.globl _b
_b:
.p2align 4,,15
L12:
lbz r0,0(r4)
addi r4,r4,1
extsb r0,r0
cmpwi cr7,r0,0
stb r0,0(r3)
addi r3,r3,1
bne cr7,L12
blr
.subsections_via_symbols
THAT's where Holub and I don't agree - he considers the long version to be
bordering on "WRONG", even for classroom blackboard! In one of his
columns, he actually CORRECTED similar code in a reader question (about
another issue) - I felt that ease of comprehension by his
readers/students should be a higher priority than "efficiency"
--
Grumpy Ol' Fred cisin at
xenosoft.com