Page 1 of 1

Fun C/C++ Hacks

Posted: Sat Feb 28, 2009 10:24 am
by nikki
They might produce less readable code, but hacks are fun. :D Just today, I was a little bored and was thinking of a way to embed statements (should be expressions, of course) directly into a 'while' condition. I came up with 'while (condition && (expression || true))'. It might not be faster, but it's cooler. :P

Here's an example:-

Code: Select all

//Absolutely fails for non-numbers.
int stringToInt(char *c)
{
    int ret = *c - '0';
    while (*++c && ((ret = (ret * 10) + (*c - '0')) || true));
    return ret;
}
Tell us about other fun hacks!

Re: Fun C/C++ Hacks

Posted: Sat Feb 28, 2009 10:26 am
by lonewolff
Nice one! 8)

I could post some stuff, but all of my code is just one continuous hack :lol:

Re: Fun C/C++ Hacks

Posted: Sat Feb 28, 2009 12:58 pm
by syedhs
Google for 'obfuscated c'.. and one example is this: (although a little too extreme):- :P

http://www.ioccc.org/2004/anonymous.c

or this:

http://www.ioccc.org/2004/burley.c

The latter link do a one statement C expression separated by comma.

Re: Fun C/C++ Hacks

Posted: Sat Feb 28, 2009 2:24 pm
by nikki
syedhs wrote:Google for 'obfuscated c'.. and one example is this: (although a little too extreme):- :P

http://www.ioccc.org/2004/anonymous.c

or this:

http://www.ioccc.org/2004/burley.c

The latter link do a one statement C expression separated by comma.
Of course. I actually downloaded 'em all, and built them and modified a few. Fun stuff there. Did you see the flight simulator?