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
-
Install 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 .
-
Add 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. [1] X Research source
- Press the Windows key and type environment . Click Edit the system environment variables in the search results. [2] X Research source
- 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 -
Open 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 . [3] X Research source
- 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
-
Use 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 .
-
Run 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. [4] X Research source
- 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.
-
Type 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. [5] X Research source
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
Tips
Submit a Tip
All tip submissions are carefully reviewed before being published
Thanks for submitting a tip for review!
References
About This Article
Article Summary
X
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
.
Did this summary help you?
Thanks to all authors for creating a page that has been read 292,759 times.
Advertisement