Page 1 of 1

Interesting short code snippets

Posted: 2023-May-15, 1:54 pm
by Manna5
I have a cool piece of C code to share with, you. This function does find the greatest common dividor using the well-known variant of Euclidean algorithm with division. The interesting thing is how it can be written using C while statement, conditional and assignment operators.

Code: Select all

int gcd (int a, int b) {
        while ((a > b) ? (a %= b) : (b %= a));
        return a | b;
You can also share your interesting snippets of code, including one-liners, in this thread.