VOOZH about

URL: https://www.geeksforgeeks.org/techtips/how-to-compile-and-run-c-program-in-terminal/

⇱ How to Compile and Run C program in Terminal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Compile and Run C program in Terminal

Last Updated : 5 Dec, 2025

Compiling and running C programs on Windows requires a proper C compiler and access to the system terminal. With MinGW (a Windows-friendly version of the GCC compiler), users can easily convert their C source code into executable files using the Command Prompt. This article will guide you through installing MinGW, configuring the environment, and compiling C programs directly from the terminal.

Key Highlights:

  • Learn how to install and set up MinGW (GCC) on Windows
  • Understand how to configure environment variables
  • Use Command Prompt (CMD) to compile .c files into executables
  • Run C programs without needing any GUI-based IDE

How to Compile and Run C Program in Terminal

To compile and run C programs on Windows, you need a compiler and a terminal. MinGW (Minimalist GNU for Windows) provides an easy-to-install GCC compiler that works perfectly with Command Prompt (CMD). Once set up, you can compile any .c file into an executable using simple commands.

Below is a step-by-step guide to install MinGW, configure system paths, and compile/run C programs effortlessly.

Step 1: Install MinGW (GCC Compiler)

Before running C programs, install a compiler that converts C source code into machine code.

  • Visit the official MinGW download page.
  • Download the installer and run it..
πŸ‘ Install_mingw

In MinGW Installation Manager, select the following packages:

  • mingw32-base
  • mingw-gcc-g++
πŸ‘ Installtion02_MinGW
  • Go to Installation β†’ Apply Changes.
  • Wait for MinGW to install all required components.
πŸ‘ Installation_MinGW

Step 2: Add MinGW to the System PATH

Adding MinGW to the PATH allows you to use gcc from any folder in CMD.

  • Press Windows Key β†’ search Environment Variables.
πŸ‘ System_Environment
  • Open Edit the system environment variables.
  • Click Environment Variables.
  • Under System variables, select Path β†’ click Edit.
  • Click New β†’ add:
    • Click OK three times to save and exit.
πŸ‘ Path-(1)

Step 3: Open Command Prompt

  • Press Windows Key, search cmd.
  • Right-click β†’ Run as Administrator.
πŸ‘ cmd_1
  • Type:
  • gcc --version

If MinGW is installed correctly, the GCC version will appear.

πŸ‘ gcc_version

Step 4: Navigate to Your C Program Directory

Use the cd command to go to the folder containing your .c file

  • Type "cd C:\MyPrograms" , Click Enter button.
πŸ‘ cd_program

Step 5: Compile the C Program Using GCC

Use this syntax to compile your program:

gcc filename.c -o filename.exe

Explanation:

  • filename.c β†’ your C source file
  • -o filename.exe β†’ creates an executable output file

Example:

gcc hello.c -o hello.exe

Step 6: Run the C program and see the output

Finally, run your program with:

  • filename.exe

Example: hello.exe

Your C program output will now appear in the terminal.

πŸ‘ exe

Output
It is Armstrong No.

Also Read

Comment
Article Tags: