To run a C program in VS Code via the terminal, you must first compile the source code into an executable file using a compiler like GCC, and then execute that file. The most common sequence of commands is gcc filename.c -o filename to compile, followed by ./filename (on macOS/Linux) or filename.exe (on Windows) to run the program. This process transforms human-readable C code into machine-readable binary code that your computer's processor can actually execute.
What You'll Need
- Visual Studio Code: The latest version installed on your system.
- A C Compiler: GCC (GNU Compiler Collection) is the standard. Windows users typically install MinGW-w64, while macOS users install Xcode Command Line Tools and Linux users use the build-essential package.
- C Extension for VS Code: The official C/C++ extension by Microsoft is recommended for syntax highlighting and IntelliSense.
- A C Source File: A file saved with the
.cextension (e.g.,hello.c).
Step-by-Step
- Open Your Project Folder: Launch VS Code and go to File > Open Folder. Select the folder where your C source code is stored. This ensures that the integrated terminal opens in the correct working directory, preventing "file not found" errors during compilation.
- Create Your C File: If you haven't already, create a new file by clicking the New File icon in the Explorer sidebar. Name it with a
.cextension, such asmain.c. Write your code, ensuring you have amain()function, and save the file (Ctrl+S or Cmd+S). - Open the Integrated Terminal: You can open the terminal by going to Terminal > New Terminal in the top menu, or by using the keyboard shortcut Ctrl+` (backtick). The terminal will appear at the bottom of your editor window.
- Verify Compiler Installation: Before running the compile command, check if your compiler is accessible in the system path. Type
gcc --versionin the terminal and press Enter. If you see a version number, you are ready. If you see an error like "command not found," you must install a compiler or add itsbinfolder to your system's Environment Variables. - Compile the Source Code: In the terminal, type the following command:
gcc main.c -o main.gccis the command to invoke the compiler.main.cis the name of your source file.-ois a flag that stands for "output."mainis the name you want to give to the resulting executable file.
- Run the Executable: Now that the compiler has created a binary file, you can run it.
- On Windows: Type
.\main.exeor simplymainand press Enter. - On macOS and Linux: Type
./mainand press Enter. The./tells the shell to look for the executable in the current directory.
- On Windows: Type
- View the Output: The result of your program will print directly in the terminal window below your code.
Common Mistakes
One of the most frequent errors is forgetting to save the file before compiling. If you make a change to your code but do not save it, GCC will compile the previous version of the file, leading to confusion when the output doesn't match your current code. Enable "Auto Save" in File > Auto Save to prevent this.
Another common mistake is naming the output file the same as the source file without the extension, but then forgetting which is which. For example, if you run gcc main.c -o main.c, you will overwrite your source code with the binary executable, effectively deleting your code. Always ensure the output name (the part after -o) is different from the source filename or lacks the .c extension.
Users often forget the ./ prefix on Unix-based systems (macOS and Linux). In these environments, for security reasons, the current directory is not in the system path. Typing just main will result in a "command not found" error, even if the file is sitting right there in the folder. You must explicitly tell the terminal to look in the current directory using ./.
Case sensitivity is also a major pitfall. C is a case-sensitive language, and most terminal shells (especially on Linux and macOS) are case-sensitive as well. If your file is named Hello.c and you type gcc hello.c -o hello, the compiler will fail because it cannot find a lowercase hello.c.
Tips
To make your development faster, use the "Up Arrow" key on your keyboard while in the terminal. This cycles through your previous commands. Instead of re-typing the long compilation string every time, you can just press the Up Arrow twice and hit Enter to re-compile and then run your program.
If you are working on a project with multiple source files, you can compile them all at once. For example, if you have main.c and functions.c, use the command gcc main.c functions.c -o myprogram. This links all the object files into a single executable.
For better debugging, add the -Wall flag to your compilation command: gcc -Wall main.c -o main. The -Wall flag stands for "Warnings: all." It tells the compiler to alert you about potential logic errors or risky coding practices that aren't technically syntax errors but could cause your program to crash or behave unexpectedly.
If you find yourself running the same commands repeatedly, consider creating a simple shell script (.sh for Linux/macOS) or a batch file (.bat for Windows) in your project folder. You can put the compile and run commands in that file and execute the script to automate the entire process with one command.
Troubleshooting
If you encounter the error "gcc is not recognized as an internal or external command," it means the compiler is either not installed or its location is not defined in your system's PATH environment variable. On Windows, you must find the bin folder of your MinGW installation (usually C:\mingw64\bin), search for "Edit the system environment variables" in the Start menu, go to Environment Variables > System Variables > Path > Edit, and add the path to that bin folder. Restart VS Code after doing this.
If the terminal says "Permission denied" when trying to run ./main on macOS or Linux, the executable may not have the necessary permissions. You can fix this by running chmod +x main in the terminal, which grants execute permissions to the file, and then trying ./main again.
When you see a long list of errors during compilation, do not panic. Always look at the very first error reported. Often, a single missing semicolon on line 10 will cause the compiler to get confused and report twenty different errors on lines 11 through 30. Fix the first error, save the file, and re-compile; the subsequent errors often disappear on their own.
If your program runs but the terminal window closes immediately (common when running the .exe outside of the VS Code integrated terminal), it is because the program finished its execution and the process ended. To keep the window open, add getchar(); or system("pause"); (Windows only) just before the return 0; statement in your main() function. This forces the program to wait for user input before exiting.
Frequently Asked Questions
Do I need to compile the program every time I change the code?
Yes. C is a compiled language, not an interpreted one. Any change you make to the .c file is only a change to the text. To see those changes in action, you must run the gcc command again to update the binary executable file.
Can I run C code without installing a compiler?
Not locally on your machine. You must have a compiler to turn C code into an executable. However, you can use online C compilers (web-based IDEs) if you only need to test small snippets of code without setting up a local environment.
What is the difference between gcc and g++?
gcc is the GNU C Compiler, primarily used for C programs. g++ is the GNU C++ Compiler. While gcc can compile C++ code, g++ automatically links the C++ standard libraries, which are required for C++ programs to function. For standard .c files, gcc is the correct choice.
Why does my program work on my computer but not on my friend's?
The executable file (the .exe or the binary) is platform-specific. A file compiled on Windows will not run on macOS or Linux because the operating systems use different binary formats and system calls. You must share the .c source code and have your friend compile it on their own machine.
Is there a way to run C code with a single button click in VS Code?
Yes, you can install the "Code Runner" extension. Once installed, it adds a "Play" button to the top right of the editor. When clicked, it automatically executes the terminal commands for you. However, learning the manual terminal commands is highly recommended for understanding how the compilation process actually works.