Linux is an operating system. It has been around since the mid-1990s. Over time its user base has expanded to all across the globe. Today, it can be found everywhere, from your phones, cars, to your TVs and refrigerators.
Ubuntu is a distribution of Linux. In this article, we will go over how to compile and run the traditional Hello World! program in C on Ubuntu 20.04. Follow the steps shown below to create your very first C program.
Opening the terminal
Click the Show Applications icon. This icon can be found at the bottom right of the launcher bar as shown in the image below.
This will take you to the Applications Menu, as shown below.
Type terminal in the search bar.
Select the terminal icon to launch it.
Checking if GCC is installed
The GNU Compiler Collection (GCC) is a collection of libraries and compilers for C and other languages like C++, Go, etc.
To check if you have the GCC compiler installed on your system, run the following command.
If you get a similar message at the end of the following screenshot, then your GCC Compiler is installed and you can move on to creating your source code file.
If you see the message shown in the image below, then you need to install the GCC Compiler.
Use the command shown below to install the GCC compiler on your system. A detailed article about GCC installation on Ubuntu 20.04 is here.
sudo apt install gcc
The first step is entering your password. Once you enter your password, the installation starts. You will next be asked to confirm the installation. Type in y to confirm the installation.
Once the installation completes successfully, you should see the following output.
Creating your C program
To create a file named “hello.c”, enter the following command in your terminal. You can name your file whatever you want, but the best practice is to use a name descriptive of the program.
When you run this command, Linux launches the text editor.
Enter the following C source code to print Hello World!
Save your file, close the text editor, and go back to the terminal.
A closer look at the Hello World! program
The first line of every C program begins with the pound sign, #. The first line is called the preprocessor directive. The purpose of the preprocessor directive is to instruct the compiler to do the required preprocessing before the actual compilation.
Next up is the main() function. This function is the starting point of every C program and you can have only one main function in your program.
Hello World! the example has two statements. A statement is an instruction in a program and it ends with a semicolon.
The printf() function prints the output on the screen.
Though the return 0; statement is not necessary for this program. The purpose of using it here is to help you get familiar with it as more complex programs need it.
You may notice how the curly braces are in separate lines, this is not necessary. C is not fussy about how text is laid out. You could write the whole code in one line and it would still work, but it isn’t the best practice. Using spaces and separate lines makes your code cleaner and easier to read.
Running your C program
There are two ways of running your source code, we will go through both here.
Enter the command below
gcc –o hello hello.c
This command invokes the GCC compiler to compile the file hello.c. The result is outputted to hello, an executable. We used hello here, you can use any other word.
The compiler creates a binary file if the source code has no errors.
Enter the following command in the terminal next:
This command loads the executable file into memory. This results in the CPU executing the instructions in it. The ./ part of the command refers to the current directory. The ./hello command loads and runs your executable file ‘hello’.
If the program runs successfully, you should see the text, “Hello World!” in your terminal as shown below.
Enter the command below
This command generates a machine-executable bytecode file, “a.out”.
You can view all the files by running the ls command
Next, run the following command
This command executes the bytecode and you will see the text “Hello World!” on the terminal.
And there you go! You just wrote your first C program. Hello World! the program may seem useless and simple, but it is the best way to get started on learning how to program. By writing this yourself, you better under concepts that may otherwise seem abstract and vague.
Maryam is a software engineer. In addition to having knowledge and experience in software development, she has a passion for technical writing.