I’ve been seeing these two parallel phrases since I started C weeks ago, need someone with the knowledge of the C compiler to tell me which leads to better code.
Version1:
char s[]="aString",*sp=&s,c;
while(c=*sp++){
operation(c);
}
Version2:
char s[]="aString",*sp=&s;
for(;*sp;sp++){
operation(*sp);
}
Alright, I understand that version2 involves some repeating differencing, so is version1 always better than version 2? if not , what are some typical exceptions?
I’d expect those to be exactly the same at any reasonable optimization level – did you try it?
Edit:
I wanted to confirm, so I tried it. Here’s my example program (I fixed your pointer mismatch errors, too):
Compiled with clang at
-O3on my Mac, here’s the object file:As you can see, literally identical.