You need to compile your program before you can run it. To do this, you'll need a C compiler, like gcc. You can install this with:

sudo apt-get install gcc

Then, to compile your program, creating an executable called file:

gcc -Wall -o file file.c

Which you should then be able to run:

./file
Answer from Jeremy Kerr on askubuntu.com
🌐
Log2Base2
log2base2.com › C › basic › how-to-compile-the-c-program.html
How to compile c program in linux using gcc
🚀 From Python to placements — 90+ courses & 200+ exams, free on Leyaa.ai → ... Type your program. ... Press Esc button and then type :wq. It will save the file. ... Download the latest Dev CPP compiler from the trusted website. Install it.
Discussions

compiling - How to compile & execute C programs under Linux? - Unix & Linux Stack Exchange
I'm migrating to from Windows to Linux and I heard a lot about Linux and how you can do some real good programming under Linux. But I'm have no idea about how to compile a code written in C on Linux. More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 24, 2012
How to compile C program with variable name using Linux terminal? - Stack Overflow
Is there any possible way to compile C program in a more efficient manner than running whole command. gcc -g -Wall file_name.c -o file_name.out For example to use make command in this way: make ar... More on stackoverflow.com
🌐 stackoverflow.com
gcc - How to compile C files in terminal - Raspberry Pi Stack Exchange
Can someone please tell me how to compile from the command line in a clear, concise way. Preferably, my programs will be written in a real text editor (such as leaf pad). ... I am a complete n00b when it comes to Linux, C, and the Raspberry Pi. More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
Is there an easy way to write/compile/run C code on windows with a chosen compiler without having to use pseudo-linux nonsense?

No, there's no solution like what you're asking for. You're working on an iPhone complaining that it doesn't function like an Android device.

It's relevant right now. Learn it.

More on reddit.com
🌐 r/learnprogramming
11
0
March 5, 2018
People also ask

What Does the -static Flag Do When Compiling a C Program?
A: The -static flag compiles the program with static linking, including all library dependencies within the executable.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › how to compile a c program in linux
How To Compile And Run a C Program In Linux | upGrad
How Can You Include Debugging Symbols in a Compiled C Program?
A: Compile the program with the -g flag to include debugging information, facilitating source-level debugging.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › how to compile a c program in linux
How To Compile And Run a C Program In Linux | upGrad
How Do You Cross-Compile a C Program for a Different Architecture?
A: Use a cross-compiler like GCC configured for the target architecture to compile your C program accordingly.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › how to compile a c program in linux
How To Compile And Run a C Program In Linux | upGrad
🌐
Jvns.ca
jvns.ca › blog › 2025 › 06 › 10 › how-to-compile-a-c-program
Using `make` to compile C programs (for non-C-programmers)
I sometimes write 5-line C programs with no dependencies, and I just learned that if I have a file called blah.c, I can just compile it like this without creating a Makefile: ... It gets automaticaly expanded to cc -o blah blah.c, which saves a bit of typing. I have no idea if I’m going to remember this (I might just keep typing gcc -o blah blah.c anyway) but it seems like a fun trick. If you’re having trouble building a C program, maybe other people had problems building it too! Every Linux distribution has build files for every package that they build, so even if you can’t install packages from that distribution directly, maybe you can get tips from that Linux distro for how to build the package.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › how-to-compile-and-run-a-c-c-code-in-linux
How To Compile And Run a C/C++ Code In Linux - GeeksforGeeks
July 23, 2025 - The methods demonstrated with CC, GCC, and G++ compilers equipped readers to compile and execute code efficiently. We discussed step-by-step instructions and example scripts. Overall, we can say that this article will help programmers to successfully run their C and C++ programs on Linux systems.
🌐
Ruc
webhotel4.ruc.dk › ~keld › teaching › CAN_e14 › Readings › How to Compile and Run a C Program on Ubuntu Linux.pdf pdf
1 How to Compile and Run a C Program on Ubuntu Linux Keld Helsgaun
Search for the terminal application in the Dash tool (located as the topmost item in the · Launcher). Open up a terminal by clicking on the icon. ... Step 2. Use a text editor to create the C source code. ... Close the editor window. ... Step 3. Compile the program.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › how to compile a c program in linux
How To Compile And Run a C Program In Linux | upGrad
August 29, 2025 - A Linux Distribution – Any popular distribution such as Ubuntu, Debian, Fedora, or Arch Linux works well for compiling C programs. Ubuntu is commonly used by beginners due to its extensive support and ease of installation.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › how to compile c program in linux?
How to Compile C Program in Linux? - Scaler Topics
April 13, 2024 - Learn to install build-essential meta-package in Linux Operating System and compile and run a C program in the terminal on scaler topics.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › gnu c / c++ › how to compile and run a c/c++ code in linux
How To Compile And Run a C/C++ Code In Linux - nixCraft
June 29, 2024 - 4 save your program as “filename.cpp” on desktop, “.cpp” is compulsory. 5 open terminal again and type “cd Desktop”. 6 In second line type “g++ filename.cpp”. 7 Type “./a.out”. NOW YOUR WILL RUN. ... Thanks! This article really helped me to find the GNU compiler in a Linux Operating System and showed me how to compile a C program.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to compile a c program using the gnu compiler (gcc)
How to Use GCC to Compile a C Program on Linux and ...
February 19, 2026 - Type gcc source_file.c -o program_name and press Enter to compile your source code. Replace source_file with the name of your source code file, and program_name with the name you'd like to give your compiled program.
🌐
Medium
medium.com › @filip.melka › your-first-c-program-on-linux-a-quick-guide-with-vim-and-vscodium-bc941228d4b2
Your First C Program on Linux: A Quick Guide with Vim and VSCodium. | by Filip Melka | Medium
November 17, 2024 - Just click the play button in the top-right corner to compile and execute your code without needing terminal commands. ... In this article, we covered the basics of writing, compiling, and running a C program on Linux using both Vim and VSCodium. Whether you prefer the terminal’s simplicity or the power of an IDE, Linux offers flexible tools to fit your workflow.
🌐
CASS
cass-kul.github.io › tutorials › compiling-c
Compiling C | CASS
No matter if you already have a Linux OS running or if you are using WSL, open a new Terminal. In WSL, this is what you already see when you open Ubuntu and log in, on normal Ubuntu, you will have to search for the Terminal program or press CTRL+ALT+T simultaneously.
🌐
It's FOSS
itsfoss.com › run-c-program-linux
How to Run C Program in Ubuntu Linux [Terminal & GUI Ways]
March 29, 2023 - Step 1: Write your program and save the file with a .c extension. For example, my_program.c. Step 2: You compile the program and generate the object file using gcc compiler in a terminal like this: ... This was just a quick summary of how to ...
🌐
San Diego State University
ost.sdsu.edu › osticket › kb › faq.php
Run a C Program in Linux
By default, gcc places the executable into this file, overwriting any existing file of the same name. You specify the name of your executable, say samplePrgoram by the following line: gcc sampleProgram.c -o sampleProgram · The final step is to run the compiled C program.
🌐
VITUX
vitux.com › how-to-write-and-run-a-c-program-in-linux
How to Write and Run a C Program in Linux – VITUX
April 20, 2023 - To compile a simple C program, we use the Linux command-line tool, the terminal.
🌐
University of Chicago
classes.cs.uchicago.edu › archive › 2017 › winter › 51081-1 › LabFAQ › introlab › compile.html
Basic compiling on the Unix
To provide basic information on how to compile your C program on Unix. This page is intended for people who want to practice programming C now. This material will be greatly supplemented later in the course. ... gcc. This is a compiler from Gnu for Linux.
🌐
Medium
salmenzouari.medium.com › how-to-compile-and-run-c-program-in-linux-using-gcc-b58ab78a5f53
How to Compile and Run C Program in Linux Using gcc? | by Salmen Zouari | Medium
September 18, 2019 - There are other programs which perform this task to get the program running. On a UNIX/Linux system, the translation from source code to object code (executable) is performed by a compiler driver.
Top answer
1 of 3
16

Do NOT use nano (or another text editor to put your code into) with root/sudo permissions (ie. do not edit with sudo nano, only use nano) if all you are doing is personal stuff that does not need superuser permissions.

Answer

To compile from the command line (assuming yourcode.c is the name of your C file, and program the name of the resulting program after compilation):

  1. Write your code in your favorite editor:

    • In the terminal, type nano yourcode.c (assuming you want to use nano);
    • Or use your Leafpad editor (and make sure to know where your file is saved).
  2. Back to the terminal, navigate to where your C file is stored. (Reminder: ls to list directory contents, cd to change directory.)

  3. Compile your code with gcc -o program yourcode.c.

  4. Execute it with ./program. Done!

Bonus method

If you intend on compiling/executing your program quite a lot, you may save yourself time by writing a Makefile. Create this file with Leafpad (or, in terminal, nano Makefile), then write:

all:
    gcc -o program yourcode.c
    ./program

(Make sure you presently use Tab for indents, and not spaces.) Then every time you just type make in the terminal (or make all, but let’s keep things short!), your program will compile and execute.


Testing

Want to make sure your GCC installation works? Copy-paste the following to your terminal:

cd /tmp
cat <<EOF > main.c
#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}
EOF
gcc -o hello main.c
./hello # Press Enter to execute your program

If it echoes “Hello World”, then you’re good to go.

2 of 3
4

Compiling C programs on the raspberry pi is rather simple. First, create your program in a text editor and save it as <insert name>.c It should be saved on the Desktop.

Next, open terminal. In it type:

cd Desktop

This changes the directory that the terminal is looking at to Desktop. This is also where our program is stored.

gcc -Wall <myName>.c -o <compiled name>

This is where the interesting stuff occurs. GCC is the compiler - it makes your code executable. -Wall activates compiler warnings - this is extremely helpful for debugging. The next line <myName>.c tells the computer where the code is stored. -o is an option - it tell GCC to compile. Lastly, <compiled name> is the name of your new program.