This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
The wikiHow Tech Team also followed the article's instructions and verified that they work.
This article has been viewed 127,952 times.
Learn more...
If you've written or downloaded some C code on your Windows PC, you'll need to compile the code to turn it into an program you can run. This simple wikiHow tutorial will walk you through compiling and running a C program from the Windows command prompt using MinGW, an easy-to-install version of the GCC compiler.
Steps
-
1Install MinGW, a simple C compiler. If you haven't already done so, you'll need to install a C compiler on your PC to compile the C program. Compiling the C code will turn the code into an executable C program. MinGW is one of the easiest options to install for Windows:
- Download MinGW from https://sourceforge.net/projects/mingw.
- Double-click the downloaded file and follow the on-screen instructions to prepare the installation.
- When you get to the MinGW Installation Manager window, you'll see several packages in the upper-right panel.
- Check the boxes next to "mingw32-base" and "mingw-gcc-g++."
- Click the Installation menu and select Apply Changes.
- Click Apply.
-
2Add the compiler's path to your system environment variables. This step makes it easier to run the compiler from the command prompt, as you won't have to enter the full path to the GCC program.
- Press the Windows key and type environment. Click Edit the system environment variables in the search results.
- Click the Environment Variables… button.
- Select the "Path" option under "System variables" and click Edit…
- Click New.
- Type C:\MinGW\bin and click OK.
- Click OK and OK again.
Advertisement -
3Open a Command Prompt window as an administrator. To do this, press the Windows key, type cmd, right-click Command Prompt, and then select Run as Administrator.
- Once the prompt window is open, double-check that the compiler installed properly (and that the environment variables are set) by running the command gcc -- version at the prompt.
- If you already had a command prompt window open, close it and open another so the path you added
-
4Use the cd command to go to the directory where your C program is saved. For example, if the program you want to compile is in C:\MyPrograms, type cd C:\MyPrograms and press Enter.
-
5Run the gcc command to compile your C program. The syntax you'll use is gcc filename.c -o filename.exe. This compiles the program and makes it executable.
- Replace filename.c with the name of the file containing your C code, and filename.exe with the name you want to give the compiled program.
- Make sure you give the compiled program a name that ends with .exe.
- The -o flag specifies the output file.
-
6Type the name of your new program and press ↵ Enter. For example, if you created a program called hello.exe, you can type hello or hello.exe. This runs your C program from the command prompt.
About This Article
1. Install MinGW.
2. Add the compiler path to your system environment variables.
3. Open an elevated command prompt window.
4. Enter the directory of the C code.
5. Type gcc filename.c -o filename.exe and press Enter to compile.
6. Run the program by typing its name and pressing Enter.