×


Steps to create and run a C program using Ubuntu 20.04 LTS ?

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.

Steps to create your very first C program on Ubuntu?

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:

gcc –v

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:

gedit hello.c

When you run this command, Linux launches the text editor.
i. Enter the following C source code to print Hello World! :

#include <stdio.h>
int main()
{ 
printf("Hello World!");
return 0;
}

ii. Then Save your file, close the text editor, and go back to the terminal.

What the Hello World! program created entails ?

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.

How to run the C program ?

There are two ways of running your source code, we will go through both here.

Method 1
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:

./hello

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.

Method 2
Enter the command below:

gcc hello.c

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:

./a.out

This command executes the bytecode and you will see the text “Hello World!” on the terminal.

[Need urgent support to configure firewall on Linux? We are available to help you today. ]


Conclusion

This article will guide you on steps to write 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. To run C program: Open #Command #prompt or Terminal(if you use #Ubuntu or Mac OS), and go to the directory where you have saved the hello. c program file. Now, to run the program, type in ./a. out and you will see Hello, World displayed on your screen. To compile and run a C program on Ubuntu Linux using the gcc compiler: 1. Open up a #terminal. Search for the terminal application in the Dash tool (located as the topmost item in the Launcher). 2. Use a text editor to create the C source code. 3. #Compile the program. 4. Execute the program.