Linux is an operating system which can now be found everywhere, from your phones, cars, to your TVs and refrigerators.
Ubuntu is a distribution of Linux.
Here at LinuxAPT, we regularly help our Customers to perform Linux related queries as part of our Server Management Services.
In this context, we shall look into how to compile and run the traditional Hello World! program in C on Ubuntu 20.04.
1. Opening the terminal
i. To begin, click the Show Applications icon. This icon can be found at the bottom right of the launcher bar.
ii. This will take you to the Applications Menu. Here, Type terminal in the search bar.
iii. Select the terminal icon to launch it.
2. 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 "Command 'gcc' not found, but can be installed with: sudo apt install gcc", then you need to install the GCC Compiler:
sudo apt install gcc
You will be requested to enter 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.
3. 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.
i. Enter the following C source code to print Hello World! :
ii. Then Save your file, close the text editor, and go back to the terminal.
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.
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.
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.