Download ArticleDownload Article

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 How.com.vn 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.

  1. How.com.vn English: Step 1 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.
  2. How.com.vn English: Step 2 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]
    • Press the Windows key and type environment. Click Edit the system environment variables in the search results.[2]
    • 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
  3. How.com.vn English: Step 3 Open a Command...
    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]
    • 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
  4. How.com.vn English: Step 4 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.
  5. How.com.vn English: Step 5 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]
    • 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.
  6. How.com.vn English: Step 6 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]
  7. 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!

      About This Article

      How.com.vn English: Stan Kats
      Written by:
      Cybersecurity Expert
      This article was written by Stan Kats and by How.com.vn staff writer, Nicole Levine, MFA. Stan Kats is the COO and Chief Technologist for The STG IT Consulting Group in West Hollywood, California. Stan provides comprehensive technology & cybersecurity solutions to businesses through managed IT services, and for individuals through his consumer service business, Stan's Tech Garage. Stan has over 7 years of cybersecurity experience, holding senior positions in information security at General Motors, AIG, and Aramark over his career. Stan received a BA in International Relations from The University of Southern California. This article has been viewed 283,722 times.
      How helpful is this?
      Co-authors: 6
      Updated: April 10, 2024
      Views: 283,722
      Categories: Windows
      Article SummaryX

      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 283,722 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement