How to compile and run a C file from the command line
To run a C file, you need two steps: compile the code into an executable program, then run that program. The compiler translates your C code into machine instructions your computer can understand. On most systems, you use the gcc compiler (or clang on Mac) to compile, then type a command to execute the result.
The basic process takes about 30 seconds once you know the commands. You'll type them into your terminal — the command-line interface on Windows (PowerShell or Command Prompt), Mac (Terminal), or Linux (any terminal process).
Key Takeaways
- Compile your C file with gcc filename.c -o programname, which creates an executable file you can run.
- Run the compiled program by typing ./programname on Mac or Linux, or programname.exe on Windows.
- If compilation fails, the terminal shows error messages that point to the line number where the problem is.
- You only need to recompile when you change the C code; running the executable again uses the old compiled version.
- Install gcc on Windows through MinGW or WSL, on Mac through Xcode Command Line Tools, or on Linux through your package manager.
Installing a C compiler on your system
Before you can compile C code, your system needs a compiler installed. Different operating systems come with different tools.
On Mac: Open Terminal and type xcode-select --install. This downloads the Xcode Command Line Tools, which includes gcc and clang. It takes a few minutes and prompts you to agree to the license.
On Windows: You have two main options. MinGW is simpler for beginners — read the installer from mingw-w64.org, run it, and follow the setup wizard. Alternatively, Windows Subsystem for Linux (WSL) gives you a full Linux environment inside Windows; type wsl --install in PowerShell as administrator, then restart your computer. Once WSL is running, open the Ubuntu terminal and type sudo apt update followed by sudo apt install gcc.
On Linux: Most distributions include gcc already. If not, open your terminal and type sudo apt install gcc (on Ubuntu or Debian) or sudo yum install gcc (on Fedora or CentOS). The package manager downloads and installs it automatically.
Compiling a C file from the terminal
Once your compiler is installed, navigate to the folder where your C file lives. In Terminal or PowerShell, use cd to change directories. For example, if your file is on your Desktop, type cd Desktop.
Then type the compile command. The standard form is:
gcc filename.c -o programname
Replace filename.c with the actual name of your file (for example, hello.c) and programname with whatever you want to call the executable (for example, hello). The -o flag tells gcc where to save the compiled program.
If your code has no errors, the compiler finishes silently and returns you to the prompt. If there are errors, the terminal displays them with the filename and line number — for example, hello.c:5:10: error: expected ';' before '}'. Fix the error in your text editor, save the file, and run the compile command again.
Running the compiled program
On Mac or Linux: Type ./programname (with the dot and slash). The dot means "current folder" and the slash separates it from the program name. So if you compiled to hello, you type ./hello.
On Windows with MinGW: Type programname.exe or just programname. Windows adds the .exe extension automatically when you compile, so both commands work.
On Windows with WSL: Use the same command as Mac or Linux: ./programname.
The program runs and displays any output to the terminal. If your C code reads input from the keyboard, the terminal waits for you to type it. When the program finishes, you return to the command prompt.
Understanding compiler flags and common options
The basic gcc filename.c -o programname command works for most straightforward programs. As you write more complex code, you may need additional flags that change how the compiler behaves.
-Wall shows all warnings, which helps catch mistakes the compiler doesn't consider errors. Type gcc filename.c -o programname -Wall. -g includes debugging information so you can use a debugger to step through your code line by line. -O2 optimizes the compiled program to run faster, though compilation takes slightly longer.
You can combine flags: gcc filename.c -o programname -Wall -g -O2. The order doesn't matter. For now, -Wall is the most useful — it catches typos and logic mistakes that would otherwise silently produce wrong answers.
What to do when compilation fails
The most common errors are syntax mistakes: missing semicolons, mismatched braces, or misspelled function names. The compiler message tells you the line number where it detected the problem. Open your file in your text editor, go to that line, and look for the mistake.
Sometimes the error is on a different line than the one reported. For example, a missing semicolon on line 5 might not be caught until line 6. Read the error message carefully — it often hints at what's wrong, like "expected ';'" or "undeclared identifier".
If you're using functions from a library (like math.h), you may need to link against that library. For the math library, add -lm to the end of your compile command: gcc filename.c -o programname -lm. The -l flag tells gcc to link a library, and m is short for the math library.
Recompiling after you make changes
Once you've compiled your program, running it again uses the same executable — it doesn't re-read your C file. If you edit the C code in your text editor and save it, you must compile again before the changes take effect.
This is a common source of confusion: you fix a bug, run the program, and the bug is still there. The reason is that you compiled the old version. straightforward type the compile command again, then run the program.
For larger projects with many files, you can use make to automate recompilation, but that's beyond the scope of straightforward single-file programs. For now, remember: edit, compile, run.
Frequently Asked Questions
What's the difference between gcc and clang?
Both are C compilers that work almost identically for basic programs. gcc is more common on Linux, clang on Mac. They produce similar results and accept the same command-line flags. If your Mac has both installed, you can use either one — just type clang filename.c -o programname instead of gcc.
Do I need to install anything on Windows if I use WSL?
WSL gives you a Linux environment, so you install gcc the Linux way: sudo apt install gcc inside the Ubuntu terminal. After that, you compile and run C files exactly as you would on a Mac or Linux machine.
Why does my program run but produce no output?
Your C code may not include any print statements. If your program doesn't call printf(), the terminal shows nothing. Add printf("Hello\n"); to your code, recompile, and run again to verify the program is actually running.
Can I compile multiple C files at once?
Yes. Type gcc file1.c file2.c -o programname to compile two files into one executable. This is useful when you split your code across multiple files, but for learning, keep everything in one .c file.
What if I forget the -o flag?
gcc creates an executable named a.out on Mac and Linux, or a.exe on Windows. You can run it with ./a.out or a.exe, but it's clearer to always use -o and give your program a meaningful name.