Page 1 of 1

What you see is...

Posted: Sat Oct 08, 2011 11:10 am
by BB+
...not what you get. Presumably if one had C code that called the rook bitboard "Bishops" and the bishop bitboard "Rooks", there would be an argument as to its "semantic" properties.

Fortunately, over a decade ago, the IOCCC (International Obfuscated C Code Contest) had an entry in a somewhat related genre.

What does this code do (broken into two parts for convenience)? http://www1.us.ioccc.org/2000/primenum.c

Code: Select all

#define BeginProgram void main(int argc, char *argv[])
#define CloseBrace }
#define CommandLineArgument -1
#define Declare int i,j,n,Flag=1;
#define EndOfProgram return;
#define False 0;
#define ForLoop ;for
#define GetCommandLineArgument n=atoi(argv[1]);
#define i F1ag
#define If if
#define Increment ++
#define Is ==
#define LessThan *(c&64)*
#define LessThanOrEqualTo !=
#define Modulo %
#define OpenBrace {
#define PossibleFactor j
#define PossiblePrime i
#define Possib1ePrime (c=getchar())
#define PrimeNumber (c^(!i*n%64));
#define Print putchar
#define SetTo =
#define SmallestPrime 2
#define True 1
#define Variables char c;
#define Zero i%j

Code: Select all

BeginProgram
OpenBrace
        Declare Variables
        GetCommandLineArgument

        ForLoop (PossiblePrime SetTo SmallestPrime ;
                 Possib1ePrime LessThanOrEqualTo CommandLineArgument ;
                 Increment PossiblePrime)
        OpenBrace
                F1ag SetTo True
                ForLoop (PossibleFactor SetTo SmallestPrime ;
                         PossibleFactor LessThan PossiblePrime ;
                         Increment PossibleFactor)
                        If (PossiblePrime Modulo PossibleFactor Is Zero)
                                F1ag SetTo False

                If (Flag Is True)
                        Print PrimeNumber
        CloseBrace

        EndOfProgram
CloseBrace
To build:
	make primenum
    To run:
	./primenum n (where n is an integer)
For 15 years, IOCCC winners have obfuscated programs using esoteric,
    difficult-to-interpret, confusing algorithms.  This entry finally bucks
    that trend and obfuscates in the most "obvious" way possible: by
    painstakenly describing the algorithm in clear pseudocode.  Even novice
    programmers will recognize that the program should find and print prime
    numbers up to a given number (as specified on the command line).

    But novice programmers especially know that mistakes are often made in
    translating pseudocode to real code, often resulting in programs with
    unexpected and confusing output. [...]
- The program attempts to mislead the reader into thinking that it
      generates prime numbers.  It does this by
        o describing a correct algorithm in pseudocode,
        o using partially correct conversion of pseudocode to real code,
        o using suggestive variable names, and
        o using a suggestive program name.
    - The program demonstrates a novel method of abusing the C preprocessor.
[...]
    - Other features include
        o useless variables
        o useless if
        o a for loop that's used as an if statement
        o dependence on the high bit being 0
    - Try also
      echo THIS MESSAGE IS IN ALL CAPS | primenum 32
See http://www1.us.ioccc.org/2000/primenum.hint for more.

Re: What you see is...

Posted: Tue Apr 03, 2012 11:51 pm
by hyatt
There was a cute obfuscated program years ago that made the rounds through rec.games.chess.computer. I think it was an obfuscated program that actually played legal chess, although I think of very poor quality...

In the code, one found this:

lots of C crammed together;return("rm -r *[7]); more crammed C

someone saw that and shouted "do not run this, it will remove all the files in your current directory, along with any sub-directories and their contents'

Now for a little analysis. In C, "rm -r *" is a pointer to a string that contains 8 characters. "r" is the zeroth, "*' is the 6th, and of course there is a trailing '\n' character that is not shown. So "rm -r *"[7] is a reference to the single byte at the end, the null character. The return therefore just returns a 0, nothing dire. But it caused a lot of discussion until someone familiar with C pointed this out...

:)

I used to follow that way back when, and there were always interesting things. Far beyond the ugly variable names like a1, a2, a3 and such...