Fun C/C++ Hacks

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
Post Reply
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Fun C/C++ Hacks

Post 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!
Last edited by nikki on Sat Feb 28, 2009 10:30 am, edited 1 time in total.
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: Fun C/C++ Hacks

Post by lonewolff »

Nice one! 8)

I could post some stuff, but all of my code is just one continuous hack :lol:
User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2703
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 51

Re: Fun C/C++ Hacks

Post 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.
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Fun C/C++ Hacks

Post 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?
Post Reply