Obfuscated C/C++

Lie in comments

This one-liner is actually taken from the How to write unmaintainable code FAQ, which you really should study before you commence with this paper...


Use the comma operator frequently, but beware of consequences. For example, the following is a very fine use of commas.

char* x = (char*) -1;
if( x = 0, delete x++, (int)x )
    // do something

Dead ends

Add code dead-ends. For example, a very good thing is to use if-expressions that will always evaluate to true or false, and then add code blocks that will never be executed, but do something altogether strange. If you have a very mean streak: Add calls to error messages there. If someone ever has to find a bug in the program, they will spend hours of looking at the code, trying to find out what went wrong, because they *know* its the error message the customer sees.

Creativity 101

Be creative with variable names. Some favourites of mine: Use combinations of o, O, and 0. Or, use all one character variables: a-z, A-Z and _. You have 53 variables right there, be sure to use all of them. Even better, try the next thing:

Use only global variables (yes, it can be done.). Then, introduce global arrays such as int _[256] and use only indices into that array for global function access.

Add unused function arguments. Or, better yet, provide function arguments but stick with global variables (i.e. ignore the actual variables totally). If you have a very mean streak, declare many varargs functions, and use them as code block separators. Pseudocode:

void WHILE(int nor,...) {}
#define DO WHILE

Trigraphs rule!

Always rely on explicit casts for arguments / return codes. The best idea is to use void* whenever possible.

Use trigraphs. All ANSI compilers support them (or at the very least, they should do so). Here they are:

Trigraph Character Trigraph Character

Tip: When using trigraphs, be sure to mix { with ??> and vice versa. Many text editors have a find-matching-brackets option; but I know no text editor that can match these.

GOTOs rule!

You can replace all expressions by gotos, and it is very easy to do so. The following is a very fine use of GOTOs to achive a simple aim: calculate the max of two arguments, by using only one return statement (return b);[Yes, this code works].

int max(int a,int b)
    goto d;
    goto e;
    goto f;
d:  if(a!=b)
    goto e;
f:  a=~b;
    goto d;
e:  return b;

Function pointers rule!

Another very fine use of function pointers is the idea to store integers not in int variables, but in function pointer variables. (Important Note: This works 100% fine for the intel / motorola architecture, I have not checked other systems). For example, would you have guessed, that the following code is a min/max function, that boils down to a simple (a<b)?a:b) expression ?

int mymax (int(*a)(int(*)(int(*)()),int(*)(int(*)(int**))), int(*b)(int(*)
(int(*)()),int*,int(*)(int(*)()))){return (int)((((int(*)(int(*)(int(*)()),int(
*)(int(*)())))a)> ((int(*)(int(*)(int(*)()),int(*)(int(*)())))b))?((int(*)(

Worse yet, to call the function you have to add explicit casts to your numberic arguments. For instance, rather than just writing mymax(3,52), here is a fine chance of adding this code:


And the best thing of all this is, that all the messy function pointers are dealt with at compilation time. Remember, a pointer - even a function pointer that gets an array of function pointers as arguments - is (on a 32-bit architecture, at least), just a plain and simple 4 byte integer address; and this is - a number. GREAT!!!!