The worst abuse of the C preprocessor (IOCCC winner, 1986)
Juan Sebastian Gonzalez
Software Developer | Full-Stack Developer | Javascript | React | Python | Nextjs | Nestjs | Fast Api | Docker | MYSQL | MONGO
Morse code is an old method to express words, sentences, or letters using only three symbols.
We could use for example sound, light, or electronic signals. Or in where is mostly used in write scenario. Using the characters: “.” “-” “ ”.
Example of use: When we use an "SOS" signal.
C Program by Jim Hague (1986)
Jim Hague (born in the UK) programmed a C program that translates ASCII code which is inserted in standard input to Morse code.
Jim Hague won the IOCCC contest in 1986. In this one, they wanted to find who can create the most confusing and unorganized code for something basic, and in this situation, the code used is mostly from the preprocess part using the precompiled.
In this situation is basically impossible to understand what the code does. And because of that, we are gonna "decode" step by step to understand what is this code about.
Like was told before this code uses basically the preprocess, so we are gonna use the corresponding command to compile the .c file till getting an executable file linked.
$gcc main.c -o h
This will cause a lot of warnings, creating a new file called h, which when we execute, we can actually write the text that we want to "translate" to morse. Just like is shown below.
We have to move left (DAH or Dash) or right (DIT or Dot) to "decode" the message. So, with this, we are able to translate any input into morse code.
领英推荐
Transforming the code
$gcc -E main.c -o hague.pr
In order to Make it, we are gonna use the first part in the compile process, which is called the preprocessing, and what this does is mainly remove comments and expand macros. So we are gonna be able to expand the #define preprocessor directive.
Like we can see in the above image we were able to get readable text.
Text transformed
The code uses a string full of letters and symbols, and this is the base to relate with the morse symbols.
In the above image, we sort a little bit the code for best understanding, now we can continue with the de codification as follow.
Functions names
At this point, we finally got a readable file that we can analyze.
What we can see is that basically, we have a .c file with the main function and another 2, that are TRANSFORM TO MORSE and PRINT_MORSE.
PRINT_MORSE is like a _putchar function and is used to print characters.
TRANSFORM_TO_MORSE purpose Is to transform every character into morse code, and when DOTS_ > 3 move DOTS_ one byte to the right side if not prints zero.
The main() function uses 3 loops.
With this, we were able to look into the possibilities of using macros and also to explore the problems that can be generated if the programmer abuses them. Then we conclude the objective of this article.
I hope the information was useful for the lector and that you can understand a little bit more about this interesting topic.