How to program in C under Linux? It really is very simple and consists of three simple steps.

Step one: You write your program and save the file with the .c extension. For example my_program.c.

Step two: You compile the program and create an object file with the gcc compiler in a terminal like this

gcc -o my_program.c

Step three: You run the generated object file to run the C program under Linux:


This was just a brief overview of how to compile and run a C program under Linux. If you are new to C or Linux I will show you these steps in detail to make you feel comfortable writing a C program in a Linux environment.

In fact, I will tell you how to run C programs in both the Linux terminal and the code editor.

Method 1: How can I run C programs on a Linux Terminal?

In order to run a C program under Linux, you must have a C compiler on your systems. The most popular compiler is gcc (GNU Compiler Collection).

You can install gcc with the package manager of your distribution. For Linux distributions based on Debian and Ubuntu, use the command apt :

install sudo apt gcc

Go to the directory where the program is stored in C (or specify a path) and then create an object file by using the :

gcc -o my_program.c

Note that providing an output object file (-o my_program) is optional. If you don’t, an object file named a.out will be created automatically. But this is not good because it is overwritten for every C program and you can’t see to which program the object file a.out belongs.

After generating an object file, run it to start the program in C. It’s already doable. Just use it like this:


And it will display the correct output if your program is correct. As you can see, this is not very different from running C++ programs under Linux.

Each time you make a change to your program, you must first compile it and then run the generated object file to run program C.

Method 2: How can I run C programs on Linux using a code editor such as Visual Studio Code?

Not everyone likes the command line and the terminal, and I understand that very well.

You can use a suitable C/C++ IDE like Eclipse or blocks of code, but these are often too heavy programs and better suited for large projects.

I recommend the use of an open source editor such as Visual Studio Code or Atom. These are mainly word processors, and you can install add-ons to compile and run programs directly from the graphical code editor.

In this example I use the Visual Studio code editor. This is a very popular open source editor from Microsoft.

First install the Visual Studio code in Ubuntu from the Program Center. Information about other distributions can be found in the package manager of your Linux distribution or in the Software Center. You can also visit the official website for more information.

Start the Visual Studio code, open/create the project and create your C-program here. I’m using an example of the Hello World program.

You need to make sure you have installed the gcc compiler on your Linux system.

install sudo apt gcc

Then you want to use an extension with which you can execute the code in C. Microsoft may ask you to install your own extension for the program in C/C++, but since it’s complicated to set up, I don’t recommend it.

I suggest using the Code Runner extension instead. It is a useless extension, and you can easily execute C and C++ code without additional parameters.

Click on the Extensions tab, search and install Code Runner.

C/C++ extension.

Restart the Visual Studio code. You should now be able to execute the code in C using one of the following methods:

  • Use the key combination Ctrl+Alt+N.
  • Press F1 and select or enter the execution code.
  • Right-click on the text editor and choose Run code from the context menu.

Right click on the program file and select Run Code

When you run a program, it is automatically compiled and then executed. You can see the output in the terminal opened at the bottom of the editor. What could be better than that?

The output of the program is shown at the bottom of the editor.

Which method do you prefer?

It is normal to run several C programs on the Linux command line, but using the code editor is much easier and saves time. Do you agree?

I’ll leave it up to you to decide which method you want to use.


Do you like what you read? Please share it with others.

Related Tags:

how to run c program in terminal mac,how to run c program in linux terminal,how to run c++ program in terminal,how to run code in terminal mac,a.out in c,how to compile c program in terminal windows,output command in linux for c program,syntax of a unix command is,all statement in c should be ended with,./a.out in linux,c program runs but does not display,c program in vi editor,linux debug executable,debugging in linux environment,debugging on linux,c++ debugger linux gui,using c debugger,how to debug c++ program in ubuntu terminal,how to run c program in ubuntu terminal,how to run a c program in terminal mac,how to run c program in terminal,how to run a program in linux terminal,how to compile c++ program in ubuntu,how to compile c program in linux using gcc,how to compile c++ program in linux using g++,c programming in linux pdf

Categories: clock tech