The basic steps: compile, then run

To run a C file in your terminal, you need two steps. First, compile the C code into an executable program using a compiler like gcc (GNU Compiler Collection). Second, run that executable. The compiler translates your human-readable C code into machine instructions your computer can actually execute.

On Linux and macOS, gcc is usually already installed. On Windows, you can install it through MinGW or use Windows Subsystem for Linux (WSL). Once you have a compiler, the process is straightforward: navigate to the folder containing your C file, compile it with one command, then run the result with another.

Key Takeaways

  • Compile your C file with gcc filename.c -o programname, which creates an executable file you can run.
  • Run the compiled program with ./programname on Linux and macOS, or programname.exe on Windows.
  • Use cd to navigate to the folder where your C file is stored before compiling.
  • If compilation fails, gcc will print error messages showing the line number and problem — read these carefully to fix your code.
  • You only need to recompile if you change the C code; running the executable again uses the already-compiled version.

Opening your terminal and navigating to your file

Open your terminal application. On macOS, this is Terminal (in Applications > Utilities). On Linux, look for Terminal or Konsole depending on your desktop environment. On Windows with WSL, open PowerShell or Command Prompt and type wsl to enter the Linux environment.

Once the terminal is open, use cd (change directory) to navigate to the folder containing your C file. For example, if your file is on your Desktop, type cd Desktop. If it's in a subfolder called "projects", type cd projects. You can type ls (or dir on Windows) to list the files in your current folder and confirm your C file is there.

Compiling with gcc

Once you are in the correct folder, compile your C file by typing:

gcc filename.c -o programname

Replace filename.c with the actual name of your C file (for example, hello.c), and replace programname with whatever you want to call the executable (for example, hello). The -o flag tells gcc to write the output to a file with that name instead of the default a.out.

If your code has no errors, gcc will finish silently and return you to the prompt. If there are errors, gcc prints them to the screen with the filename, line number, and a description of the problem. Read the error message, fix the issue in your C file using a text editor, save it, and run the gcc command again.

Running your compiled program

After compilation succeeds, run your program by typing:

./programname

On Linux and macOS, the ./ tells the terminal to look for the program in the current folder. On Windows (without WSL), type just programname.exe or programname without the dot and slash.

Your program will now execute. If it reads input from the keyboard, type your input and press Enter. If it prints output, you will see it on the screen. When the program finishes, you return to the terminal prompt.

Handling common compilation errors

If gcc says "command not found", the compiler is not installed. On macOS, install Xcode Command Line Tools by typing xcode-select --install. On Linux, use your package manager: sudo apt install gcc on Ubuntu or Debian, or sudo yum install gcc on Fedora. On Windows, install MinGW or use WSL.

If gcc says "No such file or directory" when referring to your C file, you are in the wrong folder. Type pwd to see your current location, then use cd to navigate to the correct one. If gcc reports syntax errors in your code (like missing semicolons or mismatched braces), open your C file in a text editor, find the line number gcc mentioned, and fix the problem.

Recompiling after changes

Once you have compiled your program, the executable file stays on your computer until you delete it. If you edit your C file and want to test the changes, you must compile again using the same gcc command. The old executable will be replaced with a new one that includes your changes.

You do not need to recompile just to run the program again with different input. Run the same executable multiple times with ./programname as many times as you want. Recompilation is only necessary when you change the C source code itself.

Using flags for debugging and optimization

The basic gcc filename.c -o programname command works for most cases, but gcc accepts additional flags that change how it compiles. The flag -Wall tells gcc to print all warnings, which can catch potential bugs: gcc -Wall filename.c -o programname. The flag -g includes debugging information so you can use a debugger like gdb to step through your code line by line.

For faster-running programs, use -O2 or -O3 to enable optimization, though this makes compilation slightly slower. You can combine flags: gcc -Wall -g -O2 filename.c -o programname. Start with -Wall while learning; the other flags become useful as your programs grow more complex.

Frequently Asked Questions

What is the difference between gcc and other compilers like clang?

Both gcc and clang compile C code into executables. Clang is newer and sometimes gives clearer error messages. On most systems, gcc is pre-installed, so it is the simpler choice for beginners. The command syntax is nearly identical: clang filename.c -o programname works the same way as gcc.

Can I compile multiple C files at once?

Yes. If you have two files, main.c and helper.c, compile both together with gcc main.c helper.c -o programname. For larger projects with many files, most programmers use a build tool like make instead of typing the gcc command each time.

Why does my program crash or give wrong output?

Compilation errors are caught by gcc before the program runs. If your program compiles but crashes or produces wrong results, the bug is in your C logic, not the compiler. Use a debugger like gdb to step through your code, or add printf statements to see what values your variables hold at each step.

Do I need to delete the old executable before compiling again?

No. When you run gcc filename.c -o programname again, gcc overwrites the old executable with the new one automatically. You only need to delete it manually if you want to clean up your folder.

What does the -o flag do exactly?

The -o flag specifies the output filename for the compiled executable. Without it, gcc creates a file called a.out by default. Using -o programname gives your executable a meaningful name so you remember what it does and can run it easily with ./programname.