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.

Answer from Diti on Stack Exchange
🌐
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 - To compile multiple programs at once with multiple source code files, use gcc -c file1.c file2.c file3.c. ... Run your newly-compiled program. Type ./[executable_name] but replace “[executable_name]” with the name of your program.
🌐
Log2Base2
log2base2.com › C › basic › how-to-compile-the-c-program.html
How to compile c program in linux using gcc
Press Esc button and then type :wq. It will save the file. ... Download the latest Dev CPP compiler from the trusted website.
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.

🌐
Medium
medium.com › @laura.derohan › compiling-c-files-with-gcc-step-by-step-8e78318052
Compiling C files with gcc, step by step | by Laura Roudge | Medium
February 16, 2019 - In order for our main.c code to be executable, we need to enter the command “gcc main.c”, and the compiling process will go through all of the four steps it contains. Of course gcc has options that allow us to stop the compiling process ...
🌐
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 - The following command (provided that gcc is installed on your Linux box) compiles C program helloworld.c and creates an executable file called helloworld.
🌐
iO Flood
ioflood.com › blog › gcc-linux-command
GCC Command Guide: How-To Compile C Code in Linux
December 12, 2023 - In this example, we use the gcc command to compile the ‘hello.c’ file. The -o option is used to specify the name of the output file. In this case, an executable named ‘hello’ is created.
🌐
Linux Hint
linuxhint.com › compile_c_program_linux_gcc
Compile C Program in Linux Using GCC – Linux Hint
In this article, I will show you how to install GCC and compile C programs in Linux using GCC. I will use Debian 9 Stretch for the demonstration. But I will show you how to install GCC on wide variety of Linux distributions.
Find elsewhere
🌐
Medium
steffanynaranjo.medium.com › how-to-compile-c-files-with-gcc-step-by-step-5939ab8a6c47
How to compile C files with gcc, step by step. | by Steffany Naranjo Vargas | Medium
June 11, 2020 - The following command (provided that gcc is installed on your Linux box) compiles C program helloworld.c and creates an executable file called helloworld.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › gcc-command-in-linux-with-examples
gcc command in Linux with examples - GeeksforGeeks
October 4, 2025 - There are different options in gcc command to allow the user to stop the compilation process at different stages. ... Example: This will compile the source.c file and give the output file as a.out file which is default name of output file given by gcc compiler, which can be executed using ./a.out
🌐
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
gcc -o hello hello.c · This command will invoke the GNU C compiler to compile the file hello.c and output (-o) the result to an executable called hello.
🌐
Linuxtopia
linuxtopia.org › online_books › an_introduction_to_gcc › gccintro_9.html
An Introduction to GCC - Compiling a simple C program
The classic example program for the C language is Hello World. Here is the source code for our version of the program: · We will assume that the source code is stored in a file called 'hello.c'. To compile the file 'hello.c' with gcc, use the following command:
🌐
EmbedJournal
embedjournal.com › compiling-c-programs-using-gcc
Compiling C Programs Using GCC - EmbedJournal
April 1, 2017 - The file name a.out is really annoying. especially if you are compiling more than one C program in the CWD (current working directory) then each time the new executable file will overwrite the existing one. And yeah, Linux doesn’t pop up saying “there is another file with the same name do you what to replace it” :-). Besides all these you might want to give your output file a sensible name to identify it later on. Here is how to specify the output file name during compilation and execute it likewise, $ gcc filename.c -o executable.file $ ./executable.file
🌐
Stanford
web.stanford.edu › class › archive › cs › cs107 › cs107.1194 › resources › gcc
CS107 Compiling C Programs with GCC
We generally don't want our programs named a.out, so you can give gcc an option, -o programName, to tell it what to name the runnable file: ... Note: be careful not to accidentally input a runnable file name that is the same as your input file - something like: ... On myth, your profile has been set up to catch the error and not compile it, leaving your source file in tact. This is not the case on many other Linux systems, so be careful!
🌐
CppBuzz
cppbuzz.com › c › tutorial › compile-c-program-using-gcc-on-linux
Compile C Program using GCC compiler on Linux
To use VI editor, launch the terminal on Linux and type "vi helloword.c" & press enter. Compile it using gcc command i.e "gcc helloworld.c", a.out file is created as output file.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › debian linux › how to compiling c program and creating executable file under a linux / unix / *bsd
How To Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD - nixCraft
December 11, 2017 - To compile C program first.c, and create an executable file called first, enter: $ gcc first.c -o first OR $ cc first.c -o first To execute program first, enter: $ ./first Output: ... However, both FreeBSD and Linux support direct make (GNU ...
🌐
Quora
quora.com › How-do-I-compile-a-C-program-in-Linux-using-GCC
How to compile a C program in Linux using GCC - Quora
Answer (1 of 4): This is pretty simple. The simplest way to do it. First go to the directory where you saved your c file using cd command and then execute these commands: > gcc filename.c and this command would compile your file and prepares an executable file which you can run by using : > ....
🌐
RapidTables
rapidtables.com › code › linux › gcc.html
GCC C compiler
$ gcc -static myfile.c -L/user/local/math -lmath -o execfile · Compile myfile.c with optimization and link to output file execfile:
🌐
Scaler
scaler.com › home › topics › how to compile c program in linux?
How to Compile C Program in Linux? - Scaler Topics
April 13, 2024 - Change the directory to ~/Desktop in the terminal using cd Desktop and compile the C program file hello.c using gcc hello.c -o hello command in the Visual Studio code terminal.