The basic steps to run a C file

To run a C file in your terminal, you need two steps: compile the code into a program your computer can execute, then run that program. The compiler translates your C code (the text file you wrote) into machine instructions. On Linux and Mac, the standard compiler is gcc (GNU Compiler Collection). On Windows, you'll need to install it separately or use a different compiler like MinGW.

The command to compile is straightforward. If your file is named program.c, you type:

gcc program.c -o program

This tells gcc to read program.c, compile it, and write the executable to a file called program (the -o flag means "output"). After that completes without errors, you run the program by typing:

./program

The ./ means "run the file in the current directory." If compilation fails, gcc will print error messages telling you what's wrong — usually a missing semicolon, mismatched brackets, or a function that doesn't exist.

Key Takeaways

  • Compile your C file with gcc filename.c -o outputname, where outputname is what you want to call the executable.
  • Run the compiled program with ./outputname from the same directory where you created it.
  • If gcc is not found, you need to install it — on Ubuntu or Debian use sudo apt install gcc, on Mac use Xcode Command Line Tools, on Windows use MinGW or WSL.
  • Compilation errors mean your code has syntax problems; read the error message to find the line number and fix it before trying again.
  • If your program takes input from the keyboard or reads files, make sure those files exist in the directory where you run the program.

Check if gcc is installed on your system

Before you can compile anything, you need gcc. Open your terminal and type:

gcc --version

If you see a version number, gcc is ready to use. If you see "command not found" or similar, you need to install it.

On Ubuntu, Debian, or other Linux distributions that use apt, run:

sudo apt update && sudo apt install gcc

On Mac, install Xcode Command Line Tools by typing xcode-select --install and following the prompts. On Windows, download MinGW from mingw-w64.org, or use Windows Subsystem for Linux (WSL) to get a Linux environment where gcc is easier to set up.

Navigate to the directory where your C file lives

Your terminal always has a current working directory — the folder you're "in." When you compile a C file, gcc looks for it in that directory. Use pwd to see where you are, and ls to list files in that folder.

If your file is on your Desktop, navigate there with:

cd ~/Desktop

If it's in a folder called projects inside your home directory, use:

cd ~/projects

Once you're in the right place, type ls again to confirm your C file is there. You should see something like hello.c or calculator.c in the output.

Compile the file and handle errors

With your C file in the current directory, compile it:

gcc myprogram.c -o myprogram

Replace myprogram with your actual filename (without the .c for the output name). If compilation succeeds, the command returns silently and creates an executable file. If there are errors, gcc prints them to the screen with a line number.

Common errors include missing semicolons, undefined variables, and mismatched parentheses or braces. Read the error message — it usually points to the exact line. Open your C file in a text editor, find that line, fix the problem, save the file, and run the gcc command again.

If you want to see warnings as well as errors (which can catch potential bugs), add the -Wall flag:

gcc -Wall myprogram.c -o myprogram

Run the compiled program

Once compilation succeeds, you have an executable file in your directory. Run it with:

./myprogram

The ./ tells your terminal to look in the current directory. Without it, the terminal searches your system PATH (a list of standard locations) and won't find a program you just created locally.

If your program reads input from the keyboard, type it in after you run the command. If it reads from a file, make sure that file is in the same directory. If your program produces output, you'll see it printed to the terminal.

Recompile after making changes

Every time you edit your C file, you must recompile before running it. The executable file does not automatically update when you change the source code. Edit the file in your text editor, save it, then run the gcc command again.

A common workflow is: edit in one terminal window (or in a text editor), then switch to the terminal and run gcc myprogram.c -o myprogram followed by ./myprogram. Some people use a Makefile to automate this, but for a single file, typing the gcc command each time is straightforward.

Frequently Asked Questions

What does the -o flag do?

The -o flag specifies the name of the output executable file. Without it, gcc creates a file called a.out by default. Using -o myprogram makes the executable easier to find and run.

Can I run a C file without compiling it first?

No. C is a compiled language — your source code must be translated to machine instructions before your computer can run it. Some languages like Python are interpreted (the interpreter reads and executes the code directly), but C requires a separate compilation step.

Why do I get "permission denied" when I try to run my program?

This usually means the file exists but isn't marked as executable. Run chmod +x myprogram to make it executable, then try ./myprogram again. This is rare if gcc created the file, but can happen if you copy a program from elsewhere.

What if my program needs to read a file?

Put the file in the same directory as your executable, or provide the full path to the file in your C code. For example, if your program opens data.txt, make sure data.txt is in the directory where you run ./myprogram.

Can I compile multiple C files at once?

Yes. If you have main.c and helper.c, compile both with gcc main.c helper.c -o myprogram. For larger projects with many files, a Makefile is more practical, but for a few files, listing them all on the command line works fine.