LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 June 4 2012

Joe
Member

[Exercise] C predicates

for (i=0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
    s[i] = c;

easy: Rewrite the above without using '&&' or '||'.
(less) easy: Avoid using the 'if' keyword.

Offline

#2 June 4 2012

Ra8
Member

Re: [Exercise] C predicates

a false Boolean will get the value of zero so if one is false the whole product will be zero, ie: they must be all true to be != 0

for (i=0; (i < lim-1 ) * ((c=getchar()) != '\n' )*( c != EOF)!=0; ++i)
    s[i] = c;

Offline

#3 June 5 2012

Joe
Member

Re: [Exercise] C predicates

Very elegant Ra8, that's what I had in mind.
A colleague of mine used the ternary operator:

 for(i=0; i<lim-1 ? (c=getchar()) != '\n' ? c != EOF : 0 : 0 ; ++i)
        s[i] = c;

Offline

Board footer