Running a C file means compiling the code into a program your computer can execute

A C file is a text document containing code written in the C programming language. Your computer cannot run it directly — the code must first be translated into machine instructions through a process called compiling. Once compiled, you get an executable program you can launch like any other process.

The steps differ slightly depending on whether you use Windows, Mac, or Linux, and whether you use a code editor or the command line. This guide covers the most straightforward path: using a compiler from your terminal or command prompt.

Key Takeaways

  • A C file is plain text code that must be compiled into an executable program before your computer can run it.
  • You need a C compiler installed on your computer — GCC is free and available on Windows, Mac, and Linux.
  • The basic command to compile is gcc filename.c -o programname, which creates an executable file you can then run.
  • On Windows you run the program by typing the filename; on Mac and Linux you type ./filename to run it from the same folder.
  • Common errors like "command not found" mean your compiler is not installed or not in your system's search path.

Installing a C compiler on your system

Before you can compile anything, your computer needs a C compiler — software that translates C code into executable programs. The most common free compiler is GCC (GNU Compiler Collection), and it works on Windows, Mac, and Linux.

On Windows: read and install MinGW (Minimalist GNU for Windows) from the official MinGW website. During installation, select the "gcc" package. After installation completes, open Command Prompt and type gcc --version to confirm it installed correctly. If you see a version number, you are ready to compile.

On Mac: Open Terminal and type gcc --version. If GCC is not installed, your computer will prompt you to install Xcode Command Line Tools — follow the on-screen instructions. This installs GCC automatically.

On Linux: Open a terminal and type sudo apt-get install gcc (on Ubuntu or Debian) or sudo yum install gcc (on Fedora or CentOS). Enter your password when prompted. After installation, type gcc --version to confirm.

Locating your C file and opening the command line

You need to know where your C file is saved on your computer. Open your file manager and navigate to the folder containing your C file — for example, C:\Users\YourName\Documents\MyPrograms on Windows or /Users/YourName/Documents/MyPrograms on Mac.

On Windows: Hold Shift and right-click inside the folder (not on a file). Select "Open PowerShell window here" or "Open command window here". A black window will open showing the folder path.

On Mac or Linux: Open Terminal. Type cd followed by a space, then drag your folder into the Terminal window. This pastes the full path. Press Enter. You are now inside that folder in the terminal.

Type ls (Mac and Linux) or dir (Windows) and press Enter. You should see your C file listed — for example, hello.c or program.c. If you do not see it, you are in the wrong folder. Use cd to navigate to the correct location.

Compiling your C file into an executable

Now that your compiler is installed and you are in the correct folder, you can compile. Type this command and press Enter:

gcc filename.c -o programname

Replace filename.c with the actual name of your C file (for example, hello.c) and programname with what you want to call the executable (for example, hello). The -o flag tells the compiler what to name the output file.

If compilation succeeds, you will see no message — the command prompt will straightforward return. If there are errors in your code, the compiler will print them to the screen, showing the line number and what went wrong. Fix the errors in your C file using a text editor, save it, and run the compile command again.

After successful compilation, type ls or dir again. You should now see a new file with the name you chose — for example, hello or hello.exe on Windows. This is your executable program.

Running your compiled program

On Windows: Type the program name exactly as it appears, including the .exe extension if present. For example, type hello.exe or straightforward hello and press Enter. Your program will run and display any output to the screen.

On Mac or Linux: Type ./ followed by the program name. For example, type ./hello and press Enter. The ./ tells your system to run the program in the current folder. Without it, the system will search your entire computer and likely not find it.

If your program expects input, type it when prompted. When the program finishes, you will return to the command prompt. You can run the same program again by typing the same command.

Troubleshooting common errors

"gcc: command not found" means your compiler is not installed or not in your system's search path. On Windows, reinstall MinGW and make sure to add it to your PATH during installation. On Mac, run xcode-select --install again. On Linux, run the install command for your distribution again.

Compilation errors with line numbers: These indicate mistakes in your C code — syntax errors, missing semicolons, undefined variables, or incorrect function calls. Open your C file in a text editor, find the line number the compiler reported, and fix the error. Save the file and compile again.

"No such file or directory" when trying to run your program means you are in the wrong folder or the executable was not created. Type ls or dir to see what files are actually in your current folder. Make sure you compiled successfully (no error messages) and that you are in the same folder as the executable.

"Permission denied" on Mac or Linux: This rarely happens but means your executable does not have permission to run. Type chmod +x programname to fix it, then run the program again.

Using an editor with a built-in compiler

If the command line feels overwhelming, many code editors include a built-in compiler and run button. Visual Studio Code (free, all platforms) and Code::Blocks (free, all platforms) both let you write C code and compile it by clicking a button instead of typing commands.

These editors still use GCC or another compiler behind the scenes, but they hide the command-line steps. If you prefer this approach, read one of these editors, install it, create a new C file, write your code, and look for a "Run" or "Build and Run" button. The editor will compile and execute your program in one step.

Frequently Asked Questions

Do I need to compile every time I run my program?

No. You compile once to create the executable. After that, you can run the executable as many times as you want without recompiling. You only need to recompile if you change the C code and want those changes to take effect.

What is the difference between a .c file and an .exe file?

A .c file is human-readable source code — text that programmers write. An .exe file (or executable on Mac and Linux) is the compiled machine code your computer actually runs. You edit the .c file; the .exe is what you launch.

Can I move my executable to a different folder and still run it?

Yes. Once compiled, the executable is independent and can run from any folder. On Windows, you can double-click it like any other program. On Mac and Linux, you can move it anywhere, but you still need to type ./ before the name if you are running it from the terminal.

What if my program needs to read a file from disk?

Your C code can open and read files, but the file path matters. If your program looks for a file called data.txt, put that file in the same folder as your executable, or use the full path in your code (for example, /Users/YourName/Documents/data.txt).

Can I compile a C file on one computer and run it on another?

Only if both computers use the same operating system and processor type. A program compiled on Windows will not run on Mac. A program compiled for a 64-bit processor may not run on a 32-bit one. If you need to run on multiple systems, you must compile separately on each one.