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
🌐
GeeksforGeeks
geeksforgeeks.org › techtips › how-to-compile-and-run-c-program-in-terminal
How to Compile and Run C program in Terminal - GeeksforGeeks
December 5, 2025 - Use the cd command to go to the folder containing your .c file · Type "cd C:\MyPrograms" , Click Enter button. ... Your C program output will now appear in the terminal.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › build › walkthrough-compile-a-c-program-on-the-command-line
Compile a C Program on the Command Line | Microsoft Learn
Next, verify that the developer command prompt is set up correctly. In the command prompt window, enter cl (or CL, case doesn't matter for the compiler name, but it does matter for compiler options).
Discussions

gcc - How to compile C files in terminal - Raspberry Pi Stack Exchange
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): ... Or use your Leafpad editor (and make sure to know where your file is saved). Back to the terminal, navigate to where your C file is stored. More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
How do I run my C program in Mac Terminal?
To compile a file make sure you are in the directory of the file first: cd directory/path/to/file Then to compile the code, use this: gcc filename.c -o executablename To run the app from the terminal ./executablename More on reddit.com
🌐 r/learnprogramming
11
13
July 1, 2013
How do I execute a .c file? - Stack Overflow
C files cannot be executed, they must be compiled into an executable first. ... Sign up to request clarification or add additional context in comments. More on stackoverflow.com
🌐 stackoverflow.com
Using GCC to compile C code - Stack Overflow
I installed MinGW on my Windows 8 laptop and tried to compile a C code file with gcc test.c -o test.exe the compiler gave no warnings or errors but it did not create test.exe how do i get the com... More on stackoverflow.com
🌐 stackoverflow.com
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 Terminal
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 Terminal
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 Terminal
🌐
Tildesites
tildesites.bowdoin.edu › ~ltoma › teaching › cs3225-GIS › fall16 › Lectures › basics.html
Compiling and running a C program from the terminal
If for whatever reason you prefer to work locally on your laptop or desktop: open a new terminal window and keep this window open right next to the window where you are logged in to dover. In this new window type: bid19628:~ ltoma$ cd Desktop/ bid19628:Desktop ltoma$ mkdir local-cs3225 bid19628:Desktop ltoma$ cd local-cs3225 bid19628:local-cs3225 ltoma$ mkdir startup/ bid19628:startup ltoma$cd startup Now you should see a new empty folder called local-cs3225 on the Desktop. Start "Aquamacs" or "Xemacs". Start typing your code. Save this program as "~/Desktop/local-cs3225/startup/hello.c". Compile and run as above.
🌐
Edureka
edureka.co › blog › how-to-compile-c-program-in-command-prompt
How to Compile & Run a C Program in Command Prompt?
July 23, 2024 - To compile and run your C program from the terminal without any problems, simply type hello.exe on a regular command prompt in a Windows environment or in a unix-like one, use the command ./hello.exe.
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.

🌐
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 Terminal
August 29, 2025 - You can open it using Ctrl + Alt + T on Ubuntu or search for "Terminal" in the applications menu. GCC (GNU Compiler Collection) – GCC is the most widely used compiler for C programs.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › compile-and-run-c-program-in-terminal
How to Compile and Run C program in Terminal?
Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to compile a c program using the gnu compiler (gcc)
How to Compile a C Program Using the GNU Compiler (GCC)
February 19, 2026 - 4. Type "gcc --version" to verify the GCC installation. 5. Use the "CD" command to navigate to the folder with your source code. 6. Type "gcc [program_name].c –o [executable_name]" to compile the program.
🌐
Christopher Siu
users.csc.calpoly.edu › ~phatalsk › references › howToCompileAndRunCProgramsFromCommandLine.html
How To Compile and Run C Programs From the Command Line
For example, assumming you have mySource1.c, mySource2.c, and mySource3.c, the command to compile them into a single program would be: gcc -ansi pedantic -Wall -Werror -lm mySource1.c mySource2.c mySource3.c
🌐
Scaler
scaler.com › home › topics › how to compile c program in linux?
How to Compile C Program in Linux? - Scaler Topics
April 13, 2024 - Use gcc hello.c -o hello command to compile the hello.c program and generate a hello executable file. You can see in the below image that a hello file is created in the Desktop directory. Now, let us see how to run our C program in the terminal.
🌐
YouTube
youtube.com › bro code
C compile and run a C program with cmd 🏗️ (optional video) - YouTube
compile and run a c file with command prompt#C #compile #run⭐️Time Stamps⭐️(00:00:00) intro(00:00:17) Step 0. check if you have a gcc compiler(00:00:34) Step...
Published   August 21, 2021
Views   108K
🌐
Log2Base2
log2base2.com › C › basic › how-to-compile-the-c-program.html
How to compile c program in linux using gcc
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.
🌐
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 open the terminal, you can use the Ubuntu Dash or the key combination Ctrl+Alt+T. In order to compile and execute a C program, you need to have the essential packages installed on your system.
🌐
LinuxShout
linux.how2shout.com › home › how to create, compile & run a c program in linux terminal
How to create, compile & run a C Program in Linux terminal - LinuxShout
January 23, 2021 - Steps to write, run and compile C program in Linux1. Install Compiler and other Dev tools2. Check GCC version3. Open a Text editor on Ubuntu or RHEL4. Write your first C Program in Linux terminal4. Compile with GCC5. Run C program in Ubuntu Terminal6.
🌐
CASS
cass-kul.github.io › tutorials › compiling-c
Compiling C | CASS
You can paste text by pressing CTRL+SHIFT+V or if you're using Windows, also by right clicking into the terminal window. # After you are done with the changes to the file, you exit nano by pressing CTRL+X and can confirm or deny that the file should be saved by pressing Y or N and confirming with ENTER. Now, you can compile and run your program with the following commands:
🌐
GeeksforGeeks
geeksforgeeks.org › c language › compiling-a-c-program-behind-the-scenes
Compilation Process of C Programs - GeeksforGeeks
The next step is to compile filename.i and produce an; intermediate compiled output file filename.s. This file is in assembly-level instructions. Let’s see through this file using $nano filename.s terminal command. ... The snapshot shows that it is in assembly language, which the assembler can understand.
Published   April 22, 2026