×

How to Run a C Program in Ubuntu Linux? – LinuxWays

How to Run a C Program in Ubuntu Linux? – LinuxWays


C Language is one of the most popular and old programming languages used widely by Network and System Administrators in Embedded Systems. With C Programming, the efficiency and performance of systems can be increased as it is statically typed, and rich in built-in Operators. The Linux Kernel is Written in C language which makes the Applications written in C more compatible.

This article explains how you can run a C Program in Ubuntu.

How to Run a C Program in Ubuntu Linux?

To run a C Program in Ubuntu, you need to install the “gcc” compiler as it is not pre-installed in Ubuntu. Once you add the “gcc” Compiler, you can use either the Terminal to run the C Program or you can download any code editor and run the C Program through the Ubuntu GUI. Both the methods for running a C Program in Ubuntu are discussed below.

Method 1: Running a C Program in Ubuntu Using Terminal

Running a C Program from the Terminal is easier for users as it is more flexible and efficient. C Programs can be run anywhere in the Terminal with more efficiency than running the program in a code editor. Below are the steps required to run a C Program in Ubuntu using the Terminal.

Step 1: Update Existing Packages

Before running a C program in Ubuntu, ensure that the “gcc” compiler is installed on your system. In our case, it is not installed. To install the “gcc” compiler, update the existing packages in the Ubuntu Repository with the command:

Wait for the packages to update:

Once the packages are updated, upgrade those packages with the following command:

It will ask for confirmation in between the upgrade process. Enter “Y” to continue with the installation process:

Step 2: Install Build Essential

You need to install the necessary packages for compiling software. The “build-essential” is a useful package for compiling software that includes a debugger, a “gcc” compiler, and the GNU Compiler collection. To install the “build-essential” package, use the below-stated command:

sudo apt install build-essential

It will ask for confirmation as necessary packages will be added to the system. Enter “Y” to continue with the installation process:

Step 3: Verify the GCC Compiler

Once the “build-essential” packages are added, the “gcc” Compiler will be added to your Ubuntu System. Verify the addition of “gcc” packages by checking the “gcc” compiler version with the following command:

In our case, the “11.4.0” version of “gcc” compiler is added which affirms the addition of “gcc” packages on our Ubuntu System:

Step 4: Create a C File

To create a C File, navigate to the directory you want the file in, and then use the “touch” command to create a new file. Ensure the file has the “.c” extension:

In our case, we created a new C File with the “cubuntu” File Name in the default user directory with the “touch” command:

The Command line moves to the next line indicating the file was created:

To verify the creation of the “cubuntu” file, list all the files in the Terminal using the ls command:

In the Terminal, the “cubuntu.c” file can be seen:

Step 5: Adding Code Snippet in File

Now we have to add a Program or Code snippet inside the “cubuntu.c” file which can later be executed. To open the “cubuntu.c” file in the nano editor, the “nano” command can be used:

In our case, the filename is “cubuntu.c”, so the nano command will be executed as follows:

After running the command, the text editor will open:

In the Code Editor, add the C program according to your own choice. In our case, we will specify the code below:

#include <stdio.h>

int main() {

printf(“Hello from LinuxWays! n);

return 0;

}

Once added, press the “ctrl+o” shortcut key to write the content to the file and then press the “ctrl+x” shortcut key to exit the file:

Step 6: Compile the C Program

Before running a C Program File, you have to compile it with the “gcc” compiler using the following command:

gcc <fileName>.c -o <fileName>

In our case, the filename is “cubuntu.c”. Thus to compile the file, the “gcc” command will be:

The command line moves to the next line indicating that the “cubuntu” file is compiled:

Step 7: Running the C File

Now, you can run the C file through the Terminal using the following syntax:

In our case, the executable C file name is “cubuntu”:

The C Program runs and the output is displayed on the Terminal. In our case the output “Hello from LinuxWays!” can be seen in the Terminal:

This is how you can run a C Program in Ubuntu through the Terminal.

Bonus Example

To Run another C Program in the Terminal, create the File first with the “touch” Command. In our case, we created the “cexample.c” file using the “touch” command:

The Command line moves to the following line indicating the file was created:

Now to add the C Program inside the File, use the “nano” command to open the file:

The text editor will appear where you can add the C Program:

Now, add the below C Program Code in the “cexample.c” File:

#include<stdio.h>

int main()

{

int x, y, prod;

printf(“Enter any Two Numbers: “);

scanf(“%d%d”, &x, &y);

prod = x * y;

printf(“Product of %d and %d: %d”, x, y, prod);

return 0;

}

After adding the code, press the “ctrl+o” shortcut key to write the content to the file and then press the “ctrl+x” to exit the file:

Now to run the C Program, compile it first with the “gcc” command:

gcc cexample.c -o cexample

The Command Line moves to the following line indicating the “gcc” compiles and creates a new executable file “cexample”.

To Run the “cexample” file now, use the command:

The Terminal will Run the C Program and will ask you to enter any Two Numbers. Once the numbers are added, it will display the Product, i.e. The output in the Terminal:

Method 2: Running a C Program in Ubuntu Using a Code Editor

The C Program in Ubuntu can also be executed and run in a Code Editor. In our case, we will use the popular Visual Studio Code (VS Code) Editor. Visual Studio Code is not pre-installed in Ubuntu. Follow the steps below to install Visual Studio Code:

Step 1: Installing Dependencies

Install the “software-properties-common” and “apt-transport-https” packages first before installing Visual Studio Code:

sudo apt install software-properties-common apt-transport-https wget -y

The “software-properties-common” packages ensure the easy distribution of packages whereas the “apt-transport-https” ensures the required repositories are accessed. In our case, both packages are installed to the newest version:

Step 2: Downloading VS Code With Snap

Once the packages are added, install the Visual Studio Code application with the “snap” command:

sudo snap install –classic code

Executing the “snap install” command will start downloading “Visual Studio Code”:

The Visual Studio Code once downloaded, will be installed as well and after the installation completes, the Terminal informs about the installation:

After the Visual Studio Code is installed successfully, use the “code” command to start VS Code from Terminal:

The “code” command will start Visual Studio Code:

Step 3: Configuring VS Code

You will have to configure your Visual Studio Code now before running a C Program in it. In the “Get Started with VS Code” section, select the themes according to your choice and then click on the next section until you are done with the layout, themes fonts, etc. of your Visual Studio Code:

Once you are done with the Themes, the “Start” section will appear. Click on the “New File” option to create a New File in Visual Studio Code:

A new page will open inside the Visual Studio WorkSpace. Click on the “Select a Language” option:

A drop-down menu appears:

In the Drop-down menu, click on the language you want to select. In our case, we will select the “C” option which is for C Programming Language:

Now before writing the C Program, ensure that the C Program Extensions are added to the Visual Studio Code WorkSpace. In the left navigation menu of Visual Studio Code, Click on the Extensions Option:

The Extensions Menu will appear where you can see the installed extensions on your Visual Studio Code. At the top of the Extensions Menu, click on the “search bar”:

Now type in the extension name you want to add to your Visual Studio Code. In our case, we will type C/C++ and the filtered extensions will appear. Click on the first extension option from the Menu and then click on “Install” to start installing the Extension:

After the installation of the extension completes, the extension will automatically be added to your Visual Studio Code.

Step 4: Running a C Program

Once you are done with the Visual Studio Configuration, you can now create a C Program in it. To add a C Program, open the file and add the code below:

#include <stdio.h>

int main()

{

int x, y, prod;

printf(“Enter 2 numbers for multiplicationn);

scanf(“%d %d”, &x, &y);

prod = x * y;

printf(“Multiplication of %d and %d is %dn, x, y, prod);

return 0;

}

After adding the code, in the top navigation menu of Visual Studio Code, click on the “Run” option:

A drop-down menu will appear where you can start debugging your program. Click on the “Start Debugging” option from the Menu:

The Visual Studio Code will now ask for the Configuration in which the program will be compiled and executed. Among the list, click on the “C/C++: gcc build and debug active files”:

After selecting the Configuration, your C Program will execute in the Visual Studio Code Terminal seen at the bottom of the VS Code Application:

In our program, the user is asked to enter two numbers for multiplication:

After adding both the numbers and hitting “Enter”, the Output of the C Program can be seen in the VS Code Terminal:

This is how you can Run a C Program in Ubuntu using a Code Editor like Visual Studio Code.

Conclusion

C Program in Ubuntu can be run either in the Terminal with the “gcc” command or can be run using any code editor like Visual Studio Code. Both the methods to run a C Program in Ubuntu are discussed in detail in this article.



Source link