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
Discussions

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
What is the correct way to compile c code
gcc main.c path/to/cfiles/*.c -I path/to/headers -o main Note that that is a capital i before path/to/headers not lowercase L If everything is in the same directory just do: gcc ./*.c -o main More on reddit.com
🌐 r/cprogramming
17
6
June 2, 2023
How to compile C program developed with Microsoft Visual C/C++ 6.0 in Linux system
I need to compile C program developed with Microsoft Visual C/C++ 6.0 in Linux system? Thanks, Fen More on learn.microsoft.com
🌐 learn.microsoft.com
3
0
December 12, 2025
I'm on linux. How do i compile C code to a .exe file when it automatically compiles to .out?
If you just want to run the code type "./a.out". The output file is already an executable. More on reddit.com
🌐 r/C_Programming
22
3
October 8, 2021
🌐
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.
🌐
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.
🌐
Reddit
reddit.com › r/cprogramming › what is the correct way to compile c code
r/cprogramming on Reddit: What is the correct way to compile c code
June 2, 2023 -

I've got a program that is split into header files and c files along with the main. I was told to do gcc main.c <library.c>... for every other code but i don't remember having to do that when I've done stuff with header files in the past. How are you supposed to do this. Also are you supposed to include the headerfile in the .c version that the header is declaring the functions from? Sorry for my lack of technical terms. I'm exhausted today

🌐
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.
Find elsewhere
🌐
Ruc
webhotel4.ruc.dk › ~keld › teaching › CAN_e14 › Readings › How to Compile and Run a C Program on Ubuntu Linux.pdf pdf
How to Compile and Run a C Program on Ubuntu Linux
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 5658479 › how-to-compile-c-program-developed-with-microsoft
How to compile C program developed with Microsoft Visual C/C++ 6.0 in Linux system - Microsoft Q&A
December 12, 2025 - You can install GCC using your package manager, for example: ... Compile the Code: Use the terminal to navigate to the directory containing your C source files. You can compile your program using the GCC compiler with a command like:
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.

🌐
Linux Mint Forums
forums.linuxmint.com › board index › main edition support › beginner questions
How do I compile C code? [SOLVED] - Linux Mint Forums
October 9, 2018 - A familiarity with make (may be the best way to compile multi-module/multi-file programs. The terminal – gcc is a terminal command. It would be well to get familiar with a wide host of terminal commands. The gtk and/or qt4 (or is it qt5 - I've lost the pulse on this one...) libraries. Finally, if you are just getting started with the C language but have experience with other languages, probably the best source of information is the "original book", The C Programming Language by Brian W.
🌐
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.
🌐
San Diego State University
ost.sdsu.edu › kb › faq.php
Engineering Help Desk
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.
🌐
Linux Mint Forums
forums.linuxmint.com › board index › main edition support › beginner questions
How to write, compile & run a C Program in Linux ? - Linux Mint Forums
August 10, 2011 - administrator@asus-a8jn ~ $ cd Public administrator@asus-a8jn ~/Public $ ls -l total 4 -rw-r--r-- 1 administrator administrator 112 Aug 10 20:47 hello.c administrator@asus-a8jn ~/Public $ cat hello.c /* The ubiquitous hello world program */ #include <stdio.h> void main(void) { printf("Hello, World!\n"); } administrator@asus-a8jn ~/Public $ gcc hello.c -o hello-world administrator@asus-a8jn ~/Public $ ls -l total 12 -rwxr-xr-x 1 administrator administrator 7139 Aug 10 20:48 hello-world -rw-r--r-- 1 administrator administrator 112 Aug 10 20:47 hello.c administrator@asus-a8jn ~/Public $ ./hello-world Hello, World! administrator@asus-a8jn ~/Public $ ... #include<stdio.h> main() { printf("Hello World\n"); } Save your file and exit gedit. Next compile the program using gcc, the compiler that turns a text file into a program.
🌐
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.
🌐
University of Chicago
classes.cs.uchicago.edu › archive › 2017 › winter › 51081-1 › LabFAQ › introlab › compile.html
Basic Compiling
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.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › compiling-a-c-program-behind-the-scenes
Compiling a C Program: Behind the Scenes - GeeksforGeeks
We first need a compiler and a code editor to compile and run a C Program. The below example is of an Ubuntu machine with GCC compiler. We first create a C program using an editor and save the file as filename.c In linux, we can use vi to create a file from the terminal using the command:
Published   July 23, 2025
🌐
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.