Tuesday, September 04, 2007

Echo among the hills

Have written one echo code. That's simple one. It just retunns value which are typed in console, where output condition is 0. if 0 is input, program goes end.

Many do like this.


char *p,c[0];
main(){
for(p=buff;*p=getchar();) {
if(*p=='0')break;
for(;*p!='\0';) {
printf("%c",*p);
p++;
}
}
}


Said before many times, brace {} looks kinda ugly.. That is to say, it's better to extract brace as possible as we can.

So let's see break at first. In for(operator1;operator2;operator3), the operator2 equals false(0 value) breaks this for loop. We're gonna change some with this idea.


char *p,c[0];
main(){
for(p=buff;*p=getchar(),*p-'0';)
for(;*p!='\0';)
printf("%c",*p++);
}


Yes, all braces are gone. We did it? Anything else.

Let's try make 2 for squeeze for 1 for loop.


char *p, c[0];
main() {
for(p=c;(*p=='\0')?*p=getchar():printf("%c",*p++););
}


ternary operator is really helpfull. We screw statements in the operator2 of for loop.

0 Comments:

Post a Comment

<< Home