Install MinGW on Windows to run a C program

MinGW installation

In this tutorial, you will learn how to install MinGW on Windows to run your first C program in the command prompt through the GCC compiler.

Before proceeding with the installation let’s discuss briefly what is GNU, GCC, and MinGW along with their differences to get a clear idea.

GNU

GNU, which stands for “GNU’s Not Unix”, is a Unix-like operating system and a collection of free software tools beyond just compilers that are designed to develop software programs.

Think of GNU as an all-inclusive travel kit designed for a global adventurer. This kit isn’t just any travel kit. It includes everything from maps and guidebooks to tools like compasses and multi-tools, equipping you for various travel needs and unexpected situations. In software terms, GNU provides a comprehensive set of free tools designed to support software development across different environments.

GCC

GCC stands for GNU Compiler Collection. It is a piece of GNU software that includes a compiler with frontends for multiple languages like C, C++, Java, Ada, Fortran, etc.

GCC is similar to a multi-language phrasebook in your travel kit. Just as a phrasebook allows you to communicate in multiple languages, GCC supports compiling in multiple programming languages such as C, C++, and Java. This versatility is crucial because it lets developers “speak” the right programming language for their specific project’s needs, just like using the correct language in a foreign country.

MinGW

Finally, to run GCC on Windows, you need a port of GCC that is compatible with the Windows operating system. MinGW (Minimalist GNU for Windows) provides such a port, allowing developers to use GCC and other GNU tools to compile and run native Windows applications. It contains everything needed for linking and running your code on Windows. MinGW includes a port of the GNU Compiler Collection (GCC), assembler, linker, etc. MinGW is a compiler system based on the GNU GCC and Binutils projects that compiles and links code to be run on Windows. It provides C, C++, Fortran compilers, and other related tools.

MinGW acts like a travel adapter, allowing you to use your electronic devices in countries with different electrical outlets. Similarly, MinGW adapts GNU tools and the GCC compilers to work seamlessly on Windows, enabling developers to use their familiar GNU tools to create applications native to the Windows environment. This adaptation ensures that you can still use those tools effectively in Windows.

MinGW Components Illustration

Install MinGW on Windows

To install MinGW on Windows, go to https://sourceforge.net/projects/mingw/ and click on the Download Button.

Install MinGW on Windows: Step 1
Install MinGW on Windows: Step 1

When the download is complete, click the .exe file. Then click on Install.

Install MinGW on Windows: Step 2
Install MinGW on Windows: Step 2

The default installation directory is C:\MinGW. You can click on Change to change the installation directory. Then click on Continue.

Install MinGW on Windows: Step 3
Install MinGW on Windows: Step 3

When the MinGW installation is complete, click on Continue.

Install MinGW on Windows: Step 4
Install MinGW on Windows: Step 4

Now we will install the basic GCC Compiler for C by clicking on the mingwn-base checkbox and clicking on mark for installation. Then click on installation and click Apply Changes.

Install MinGW on Windows: Step 5
Install MinGW on Windows: Step 5

Click on Apply.

Install MinGW on Windows: Step 6
Install MinGW on Windows: Step 6

When the MinGW installation is complete, click on Close.

Install MinGW on Windows: Step 7
Install MinGW on Windows: Step 7

Now we will add the path to MinGW to system environment variables so that we don’t have to give the full path to the GCC compiler when we run the compiler from the command prompt. Use the following steps to add the path to MinGW to system environment variables:
Type environment variables in the search bar of Windows and click on edit the system environment variables.

Install MinGW on Windows: Step 8
Install MinGW on Windows: Step 8

Click on the environment variables.

Install MinGW on Windows: Step 9
Install MinGW on Windows: Step 9

From the system, variables click on the Path and select Edit.

Install MinGW on Windows: Step 10
Install MinGW on Windows: Step 10

Click on New to add a new path.

Install MinGW on Windows: Step 11
Install MinGW on Windows: Step 11

Paste the path to the bin folder where your GCC compiler is located. It is located inside the bin folder of MinGW.

Install MinGW on Windows: Step 12
Install MinGW on Windows: Step 12

Check if MinGW has been Installed Successfully

Open the command prompt and type gcc –version to check if the GCC is successfully installed and working.

Check a successful installation of MinGW

Compile and run your first C program through Command Prompt

Now open notepad and paste the following code:

#include <stdio.h> 
int main() 
{    
    printf("Hello, World!");         
    return 0; 
}
  • Save your file as program.c (or any name followed by the .c).
  • Open cmd and go to the directory where your C program is located. In case the program is located on desktop type: cd desktop
  • To compile your C program into object code, using the GCC compiler type the following command in cmd.
gcc -c source_file_name -o object_file_name

Where the object file name is the name you wish to name the object file after compiling your program.
In our case, we will write:

gcc program.c -c prog

This command will compile your program and create and object file with the name “prog”.
Now to convert the object file into an executable file, use the following command.

gcc prog.o -o p

Where “p” is the name of the executable file.
Now to execute the file simply type the name of the executable file and hit enter.

p
Compile and execute a C program

If you want to create an executable file directly you can use the following command:

gcc program.c

If you don’t specify any name for the executable file, by default its name will be a.

Execute a C program
Scroll to Top