How to write and run C programs in Linux

C is one of the oldest, most widely used programming languages ​​in the world. It has been used to develop countless applications, from operating systems to embedded devices. Even today, many developers still rely on C for its versatility and reliability as a programming language.

Install C Compiler (GCC) on Linux

The steps and commands mentioned in this article are on Ubuntu 22.04 LTS system, but it also works the same on other versions like Ubuntu 20.04 or Debian 11.

To compile a simple C program, we use the Linux command line tool, Terminal. To open the terminal, you can use Ubuntu Dash or the key combination Ctrl+Alt+T.

Install the necessary build packages

To compile and execute a C program, you need to install essential packages on your system. Enter the following command as root in your Linux Terminal:

$ sudo apt install build-essential

How to write and run C programs in Linux Picture 1

You will be asked to enter the root password; The installation process will then begin. Please make sure you are connected to the Internet.

Write a simple C program

After installing the necessary packages, write a simple C program.

Open Ubuntu's graphical Text Editor and write or copy the following sample program into it:

#include int main() { printf("nA sample C programnn"); return 0; }

Then save the file with the extension .c. This example names its C program sampleProgram.c

How to write and run C programs in Linux Picture 2

Additionally, you can write a C program through Terminal in gedit as follows:

$ gedit sampleProgram.c

This will create a .c file to write and save the program.

Compile C programs with GCC Compiler

In your Terminal, enter the following command to create an executable version of the program you wrote:

Syntax:

$ gcc [programName].c -o programName

For example:

$ gcc sampleProgram.c -o sampleProgram

How to write and run C programs in Linux Picture 3

Make sure your program is placed in the Home folder. If not, you will need to specify the appropriate paths in this command.

Run the program

The final step is to run the compiled C program. Use the following syntax to do so:

$ ./programName

For example:

$ ./sampleProgram

How to write and run C programs in Linux Picture 4

You can see how the program is executed in the example above, displaying the written text to print through.

This article shows you how to write, compile, and run a simple C program in Linux. All you need are the necessary packages and skills to turn you into a programming expert in Linux!

3.5 ★ | 2 Vote

May be interested

  • How to kill unresponsive programs in LinuxHow to kill unresponsive programs in Linux
    although linux software often causes fewer problems when operating, there are times when the best application also crashes. this article will show you some ways to kill unresponsive programs in linux.
  • How to Run Linux GUI Applications with WSL2 on WindowsHow to Run Linux GUI Applications with WSL2 on Windows
    windows 11 has a lot of new features, but one particularly welcome feature is the windows subsystem for linux or wsl. wsl users can now run graphical linux applications as well as standard command line programs.
  • How to run Windows applications on Linux with WineHow to run Windows applications on Linux with Wine
    there are a number of applications from windows that are needed but there are no alternatives on linux. fortunately, there is a software project that allows you to run windows applications on linux. this software is called wine. it creates a compatibility layer for windows programs to interact with the linux operating system.
  • How to Type Less than or Equal toHow to Type Less than or Equal to
    alt codes can help you write mathematical signs like the 'less than or equal to' symbol in various programs. the process of writing these signs is different between operating systems, but the same across programs. for example, the way to...
  • How to Uninstall Programs in Linux MintHow to Uninstall Programs in Linux Mint
    the linux mint operating system offers thousands of different programs and applications. but what happens when you want to uninstall one? here's how to do it! click menu. go to the applications you want to remove. right click on the...
  • Binary and Source Package: Which should be used?Binary and Source Package: Which should be used?
    what are the binary package and the source package? why should you use these two packages to install programs on linux?
  • AI content generation commands for all genres such as stories, literature, newspapers...AI content generation commands for all genres such as stories, literature, newspapers...
    these are about 100 commands divided into groups of topics related to content creation. you just need to change the topic keyword to be able to use it immediately for many different purposes.
  • How to Install and Use Homebrew on LinuxHow to Install and Use Homebrew on Linux
    although different linux distributions come with their own package managers, you will need homebrew on your system if you want to install programs that are not available in the default linux distro repositories.
  • How to install Windows Game on Linux with WinepakHow to install Windows Game on Linux with Winepak
    flatpak is a favorite choice of third-party vendors and proprietary software developers because it allows them to package linux programs once and distribute them on all linux versions.
  • How is Arch Linux different from other Linux versions?How is Arch Linux different from other Linux versions?
    arch linux is arguably the most misunderstood linux distribution. many people find arch difficult to install and maintain.