The basic steps to run a C file
To run a C file in your terminal, you need two steps: compile the file into a program your computer can execute, then run that program. The compiler reads your C code and translates it into machine instructions. On Mac and Linux, the compiler is usually already installed. On Windows, you may need to install one first.
The most common compiler is gcc (GNU Compiler Collection). The command to compile a file named program.c looks like this:
gcc program.c -o program
This tells gcc to read program.c, compile it, and write the result to a file called program (the -o flag means "output"). After that command finishes without errors, you run the program by typing:
./program
The ./ tells your terminal to look in the current folder for a program named program and execute it.
Key Takeaways
- Compiling and running are two separate steps: first gcc program.c -o program to create the executable, then ./program to run it.
- The -o program part names the output file; without it, gcc creates a file called a.out by default.
- On Windows, you need to install a compiler like MinGW or use Windows Subsystem for Linux, since gcc does not come built in.
- If compilation fails, the compiler prints error messages telling you which line has the problem and what is wrong.
- Make sure your C file is in the same folder where you opened your terminal, or use the full path to the file.
Checking that gcc is installed
Before you compile, verify that gcc is available on your system. Open your terminal and type:
gcc --version
If you see a version number, gcc is installed and ready. If you see "command not found" or similar, you need to install it.
On Mac, the easiest way is to install Xcode Command Line Tools. Open Terminal and type:
xcode-select --install
A dialog will appear asking you to install the tools. Follow the prompts and wait for it to finish.
On Linux (Ubuntu or Debian), use your package manager:
sudo apt-get install gcc
On Windows, read and install MinGW from the MinGW website, or use Windows Subsystem for Linux (WSL) and install gcc inside it. WSL is simpler if you plan to do a lot of command-line work.
Understanding compiler errors
When you run the compile command, gcc reads your code and checks for mistakes. If it finds any, it stops and prints error messages instead of creating the executable file. Each error message includes the line number where the problem is, the type of error, and a brief explanation.
Common errors include missing semicolons, mismatched parentheses, undefined variables, and type mismatches. For example, if you forget a semicolon at the end of a line, gcc might say:
program.c:5:10: error: expected ';' before 'int'
This tells you the error is on line 5, around column 10, and that a semicolon is missing. Go back to your C file, fix the mistake, save it, and run the compile command again.
Naming the output file
The -o flag lets you choose what to call the compiled program. You can name it anything you want:
gcc program.c -o myapp
This creates a file called myapp instead of program. You would then run it with ./myapp.
If you do not use -o, gcc automatically creates a file called a.out:
gcc program.c
You would run this with ./a.out. Naming your output file something meaningful makes it easier to remember what each program does, especially when you have multiple C files in the same folder.
Running programs with input and output
Many C programs read input from the user or write output to files. When you run your program, you can pass information to it in several ways.
If your program expects you to type something while it runs, just run it normally and type when prompted:
./program
If your program takes command-line arguments (values you pass when you start it), include them after the program name:
./program argument1 argument2
Your C code reads these using the argc and argv parameters in the main function. If your program writes output to a file, it does that automatically when you run it. You can also redirect output to a file from the terminal:
./program > output.txt
This saves everything your program prints to a file called output.txt instead of showing it on screen.
Compiling multiple files
Larger C projects often split code across multiple files. If you have main.c, helper.c, and helper.h, compile all the C files together:
gcc main.c helper.c -o program
List every .c file you want to include, but not the .h files (header files are included automatically when your code references them). The compiler combines all the object code and creates a single executable.
If compilation fails because of missing functions or undefined references, make sure you have listed all the C files that contain those functions. If you are using external libraries, you may need to add flags like -lm for the math library:
gcc program.c -o program -lm
Frequently Asked Questions
What is the difference between gcc and other compilers?
gcc is free and works on Mac, Linux, and Windows (via MinGW or WSL). Other compilers like clang and Microsoft Visual C++ exist, but gcc is the most common for learning and open-source projects. They all do the same job: turn C code into an executable program.
Why do I get "command not found" when I try to run my program?
The most common reason is that you forgot the ./ before the program name. Your terminal looks for programs in specific system folders by default, not in your current folder. The ./ explicitly tells it to look in the current directory. Another reason is that compilation failed and the executable was never created.
Can I edit my C file while the program is running?
No. Once you compile, the executable is a separate file. You can edit the C file, but those changes do not affect the running program. You must recompile after making changes to see them take effect.
What does the -Wall flag do?
The -Wall flag tells gcc to print warnings about potential problems in your code, even if they do not prevent compilation. Use it like this: gcc -Wall program.c -o program. Warnings help you catch bugs early, though they do not stop the compiler from creating the executable.
How do I stop a program that is running in the terminal?
Press Ctrl+C on your keyboard. This sends an interrupt signal to the program and usually stops it when ready. If the program is stuck in an infinite loop or waiting for input, Ctrl+C is the standard way to exit.