Liste des Groupes | Revenir à cl c |
On 23/12/2024 08:46, David Brown wrote:Sure. It was merely another example of something you could use, if you had ruled out simpler things (like the conversion to bool that I used, or the ! operator that you suggest).
Tim ruled out &&, ||, ?:, goto, break, continue, if, for, while, switch, do, labels, setjmp and longjmp.Isn't this just !n ? I don't think "!" was ruled out. This would also work for negative n.
>
He didn't rule out recursion, or the relational operators, or any other part of C.
>
>
int fact(int n);
>
int fact_zero(int n) {
return 1;
}
>
int n_fact_n1(int n) {
return n * fact(n - 1);
}
>
int fact(int n) {
return (int (*[])(int)){ fact_zero, n_fact_n1 }[(bool) n](n);
}
>
>
There are additional fun things that can be done using different operators. For an unsigned integer "n" that is not big enough to wrap, "(n + 2) / (n + 1) - 1" evaluates "(n == 0)".
No, I would not say so - as long as the standard library is not ruled out, it is part of C. But I think you could reasonably argue that allowing the standard library makes this whole pointless exercise even more pointless!And Tim did not rule out using the standard library, which would surely open up new possibilities.printf (not sprintf) would be reasonable here to show results. Anything else could be considered cheating.
The original context was a small subset of C that can be used to represent a larger subset.
Les messages affichés proviennent d'usenet.