Random IATA/airport code generator in C

Before working for the TUI Group, one of Europe’s leading tour operators, SSH for me was just the standard method to securely log into a remote computer. But, as it turns out, SSH is also a location on planet Earth, namely Sharm El Sheikh International Airport in Egypt.

SSH - the airport, not the Secure Shell - is one of the many three-letter codes invented by the International Air Transport Association, or IATA in short. All existing airports are designated with a three-letter code.

IATA codes do also exist for airlines. However, those are made up of only two letters. Examples are SN (Brussels Airlines), LH (Lufthansa), OS (Austrian) and QR (Qatar Airways). Use this link if you want to search for a specific airport or airline code.

Following this introduction, we will now write a C program that spits out a three-letter IATA airport code on demand. And the good news is that it doesn’t even take that many lines of code!

Please do note that IATA airport codes follow strict conventions. In other words, you can’t just randomly generate a code and start using it. At the very least the name of the city where the airport is located should be hinted at in the code (e.g. MUC = Munich; BKK = Bangkok) but let’s forget this rule for the sake of this article. The general idea is to have fun while programming, not to restrict our imagination.

Enough talking, let’s start! First create a new directory which will contain all our code and then move into said directory.

1$ mkdir iata_gen
2$ cd iata_gen

Important: don’t copy/paste the $ character! $ is only there to indicate that you should type these commands as a normal user. Also, I will include comments for some of the most important source lines of code (SLOC) as a source of documentation.

Now that you are in the iata_gen directory, go ahead and create a new file called iata_gen.c. Copy and paste in the code below.

 1/*
 2 * IATA/airport code generator in C.
 3 */
 4
 5#include <stdio.h>
 6#include <stdlib.h>
 7#include <time.h>
 8
 9#define MAX_CHAR 3
10
11void
12iata_gen(char *code)
13{
14        for (int i = 0; i < MAX_CHAR; i++) {
15                /*
16                 * Using anything other than 'A' as the starting
17                 * point will cause weird character issues, so
18                 * don't do it!
19                 */
20		        code[i] = 'A' + (rand() % 26);
21	    }
22
23        /*
24         * Without assigning the null terminator below,
25         * the code string might return weird results.
26         */
27        code[MAX_CHAR] =  '\0';
28}
29
30int
31main()
32{
33        /* Make sure to include the SLOC below, otherwise
34         * the generated IATA code will be the same each time.
35         * This guarantees uniqueness.
36         */
37        srand(time(NULL));
38
39        /*
40         * "[MAX_CHAR + 1]" is needed because we're using
41         * a null terminator.
42         */
43        char code[MAX_CHAR + 1];
44        iata_gen(code);
45
46        printf("IATA code: %s\n", code);
47
48        return 0;
49}

Translation: we create a function called iata_gen with no return type using a loop that iterates over the indices of the code array to fill it with random uppercase letters and pass it through the main() function. We set the MAX_CHAR preprocessor directive to 3 but feel free to change the value to 2 if you want to generate a two-letter airline code instead of a three-letter airport code. The time.h and stdlib.h header files are both needed because we’re nesting the time() function inside the rand() function to generate a unique seed code each time we run the program.

Next step is to create a Makefile to automate the most boring part: software compilation! Just call the file Makefile. Don’t call it another name or else the following commands will all fail. Copy and paste in the code below.

 1# Use "CC = clang" if you don't have gcc.
 2CC = gcc
 3CFLAGS = -Wall -Wextra
 4SRC = iata_gen.c
 5TARGET = iata_gen
 6PREFIX = /usr/local/bin
 7
 8all: $(TARGET)
 9
10$(TARGET): $(SRC)
11	$(CC) $(CFLAGS) $(SRC) -o $(TARGET) 
12
13install: $(TARGET)
14	install -m 755 $(TARGET) $(PREFIX)
15
16uninstall:
17	rm -f $(PREFIX)/$(TARGET)
18
19clean:
20	rm -f $(TARGET)
21
22.PHONY: all clean install uninstall

Now run the command below from within the iata_gen directory if you want to compile the source code.

1$ make

And run this command as root if you want to be able to run the program from any directory on your computer:

1$ doas make install

Be aware that I use app-admin/doas (OpenBSD fan here) for root privileges instead of app-admin/sudo but feel free to use sudo if that’s what you’re used to.

To run our freshly compiled program from within the iata_gen directory, simply type:

1$ ./iata_gen

If you compiled the program with doas make install, simply run:

1$ iata_gen

See screenshot below for the final result…

IATA/airport code generator

I wrote this tutorial for *nix-based operating systems. If you use Microsoft Windows, which is not *nix-based, you will need to change a few things (for example, I’m not sure make and Makefile exist). Unfortunately, this goes beyond the scope of this tutorial.

And we’re done. I hope this article has been educational and fun. Enjoy your flight!

#IATA   #Airport   #Airline   #Code   #Flight   #Gentoo Linux   #English