Worst abuse of the C preprocessor (IOCCC winner, 1986)
What Does International Obfuscated C Code Contest (IOCCC) Mean?
The International Obfuscated C Code Contest (abbreviated IOCCC) is a computer programming contest for the most creatively obfuscated C code, with this rules
- write the most Obscure/Obfuscated C program within the rules.
- show the importance of programming style, in an ironic way.
- stress C compilers with unusual code.
- illustrate some of the subtleties of the C language.
- provide a safe forum for poor C code. :-)
***Obfuscated: to be evasive, unclear, or confusing
***Like this obfuscated code for exemple:
AND the winner in 1986 was Jim Hague (UK) with this code:
***This program takes a string of ASCII characters input on the terminal and converts it to Morse code. :)
So when we compile the program
A lot of errors but still works (the program was written for the IOCCC and won in 1986, it is normal that some errors may appear).
Now we execute the created file:
The program transforms ASCII text to morse code, Morse Code represents letters of the alphabet, numerals, and punctuation marks by an arrangement of dots, dashes, and spaces.
Now we know what he does, but not how he does it ;)
We need to understand macros, A macro is just a piece of code that is given a name. It is replaced in the preprocessor step of the compilation process with the value that it is given, we can do this with this command.
gcc -E file name
Let’s try to replace the macros:
Now the code looks like:
let’s try to improve and comment on the code friends:
we can see how this code works but still looks like spaghetti code, We see the three functions we expected: main
, _DAH
, and __DIT
. We also see an external variable __DAH__
, a long string. __DIT
looks like the write function _DAH
It is a recursive function, as long as the argument is a number that takes more than 2 bits to write, it calls the function again, stripping the number from its last bit. The output will be part of the argument printed as —
and .
masking for 1
and 0
, i.e. the number in binary format, and it will return the second leftmost digit. As an example, if we call _DAH(5)
, 5 being 101
in binary, it will call _DAH(2)
if we change the variable names, maybe it seems more familiar.
Writing code can sometimes be the most difficult part of any software development process. If you don’t organize everything from the start — especially for big projects — the coding processes and code management afterwards may end up not just time consuming, but also a big headache.
Please follow this tips:
- Use a Coding Standard
- Write Useful Comments
- Avoid Global Code
- Use Meaningful Names
- Meaningful Structures
- Try a Version Control Software
- Use a Testing Framework