How to Write and Run a C Program in Linux

Linux C Programming

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

The C Programming Language

C is a highly portable language. This means that programs written in C are easily transferable from one computer system to another without having to be rewritten from scratch. This allows developers to quickly port their applications across different platforms without too much extra effort or hassle.

Also, C offers an outstanding balance between high-level abstractions and low-level programming techniques. In other words, you can use pre-defined functions and libraries that handle more complex tasks like memory management or string manipulation while still being able to access the machine’s hardware directly if needed. This makes it suitable for almost any application ranging from large commercial projects to smaller hobby projects!

C has been around for over 40 years, so plenty of resources are available online and offline that provide helpful tutorials and detailed documentation on how best to use the language. Whether you’re just starting with programming or already an experienced programmer looking for some tips – you should have no trouble finding what you need when looking into C development!

Finally, unlike many other modern programming languages, C isn’t tied to just one platform or software framework – it can be used with almost every major Operating System (OS), including Windows, macOS, Linux, and even Android! This universality makes it an extremely versatile option compared to other languages, which may only be ideal for certain OSes or environments.
C is truly one of the most significant programming languages ever created, and its popularity doesn’t seem like it will diminish anytime soon! With its flexibility, portability, and ease of use – it’s no wonder why so many people choose this powerful language when building their software projects!

Install C Compiler (GCC) on Linux

We have run the steps and commands mentioned in this article on an Ubuntu 22.04 LTS system, but it 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, the terminal. To open the terminal, you can use the Ubuntu Dash or the key combination Ctrl+Alt+T.

Install the build-essential packages

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

$ sudo apt install build-essential

Install gcc compiler that is part of build-essential package with apt

You will be asked to enter the root password; the installation will begin after that. Please make sure that you are connected to the internet.

Write a simple C program

After installing the essential packages, let us write a simple C program.

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

#include int main()

Then save the file with .c extension. In this example, I am naming my C program as sampleProgram.c

Example C program

Alternatively, you can write the C program through the Terminal in gedit as follows:

$ gedit sampleProgram.c

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

Compile the C program with GCC Compiler

In your Terminal, enter the following command to make an executable version of the program you have written:

$ gcc [programName].c -o programName
$ gcc sampleProgram.c -o sampleProgram

Compile source code with gcc

Make sure your program is located in your Home folder. Otherwise, you will need to specify 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
$ ./sampleProgram

Start our compiled C program

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

This article taught you how to write, compile and run a simple C program in Linux. All you need is the essential packages and skills to make you a programming guru in Linux!

Search

About This Site

Vitux.com is a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials