Download and Install C/GCC Compiler for Windows

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Compilers are used to convert the source code into machine-readable code so that the computer can understand it. For every programming language, we have to set up a compiler.

In C language, there are two ways to set up a compiler. The first one is installing the C/GCC compiler manually, and the second is installing Code::Blocks or any IDE(Integrated Development Environment) and within that, including the GCC compiler during installation.

After installing the compiler, we need to set its path to environment path variables because it allows the C program to compile from any directory on your computer.

Before reading this article, read these C Programming articles:

Source Code Editors

The source code editor is a text editor tool designed specially to edit or write the source code of any programming language. There is a basic source code editor present in Windows, i.e., Notepad, but it has limited features; therefore, for better formatting and features like multiple tabs, and plugins, you can use other editors like:

  1. TextPad (for Windows only): It is a powerful, general-purpose editor for plain text files. We can easily type the C program in TextPad. You can open Project folders inside the textpad so that you don't have to open files again and again, like in notepad.

  2. Notepad++ (for Windows only): It is a text editor for Microsoft Windows. Unlike notepad, it supports multiple tabs.

  3. VS Code: (for Windows, Mac, and Linux) Visual Studio Code gives you suggestions to auto-complete the words. It has an inbuilt debugger to trace each line of code.

  4. ATOM: (for Windows, Mac, and Linux): Atom helps you write code faster with a smart and flexible autocomplete.

  5. Sublime Text: Sublime text is a free source code editor with expandable functionality using plugins. It supports almost all programming languages (such as Python, Java, C++, etc.), is community-built, and is maintained under a free license.

Installing C/GCC Compiler for Windows

C/GCC Compiler

Following are the steps to download and install the MinGW GCC Compiler for windows.

Step 1: Search MinGW C Compiler on the Web
To download the MinGW compiler, go to your favorite browser and search MinGW C Compiler or click on the sourceforge.net link.

Search MinGW C Compiler

Step 2: Download MinGW.
After clicking on the green-colored download button on the website, the MinGW setup file will start downloading.

Download MinGW C Compiler

Step 3: Locate the MinGW-get-setup.exe File and Start Installation.

Locate the setup.exe file on your Downloads folder and double-click on it.

Locate MinGW C Setup

After double-clicking on the setup file, MinGW Installation Manager Setup Tool will now open. It will show the information like version, name, etc. Click on the Install button and proceed to start the installation.

Install MinGW C

Step 4: Specify Installation Preferences.
Now the installation manager will ask you to specify the installation preferences. For that, you will be asked to choose the installation directory. If you wish to change it, you can browse the explorer and specify the location as per your requirement. After that, click on continue to proceed further.

It is recommended to install it in the default location

MinGW C Installation

Step 5: Download and Set up MinGW Installation Manager.
The installer will now automatically download the required files for MinGW to install on your Windows system. Grab a cup of coffee and wait patiently till the installation manager finishes downloading all the files. When it is done, click on continue to proceed ahead.

Note: Active internet is required throughout the installation process.

Download and setup MinGW

Step 6: Select Packages Required for the Compiler.
There are three packages required for the basic MinGW setup that you have to choose from the MinGW Installation Manager.

1. MinGW32-base Package.
First, you have to install the MinGW32-base package. This package is used to compile the C program, including linker and other binary tools. Right-click on the MinGW32-base option and select Mark for Installation.

MinGW32-base Package

2. MinGW32-gcc-g++ Package.
Now you have to install the MinGW32-gcc-g++ package. This package is used to compile C++ source code. This is an optional component of the MinGW Compiler. It is only required if you are going to program in C++ language only. To select the MinGW32-gcc-g++ right-click on it and select Mark for Installation.

MinGW32-gcc Package

3. MinGW32-gcc-objc package.
At last, you have to install the MinGW32-gcc-objc package. This package is used to compile objective C language. It is an optional component. It is only required if you are going to program in objective C. To select the MinGW32-gcc-objc package, right-click on it and select Mark for Installation.

MinGW32-gcc-objc package

Step 7: Apply the Changes
After selecting all the required packages, go to Installation>>Apply Changes and click on Apply Changes.

Apply changes to MinGW

Step 8: Download the Changes.
Now it is time to download all the packages you selected in the previous step. Click on Apply and proceed further to download and install them.

Download changes to MinGW

The download for the packages will now begin, as shown in the window below. Wait for a few minutes until the download completes.

Download Packages for MinGW

Step 9: Installation Completed.
Now the installation has been completed, click on Close to close the Installation manager.

MinGW Installation Complete

Now the installation of MinGW is finished. To check if it is installed or not, open Command Prompt and type g++ --version.

Installing MinGW Complete

Currently, the command prompt cannot detect the MinGW compiler (GCC) because the environment path variable has not been set. The environment path variable helps to detect the compiler in your whole system. It makes the alias name for the compiler, which denotes the path. Follow the steps below to set the environment path variable for MinGW on the Windows system.

Setting up Path Variable

To set up the path for the C compiler for windows, follows the below steps :
Step 1: Copy the path of the MinGW bin.
When you install the MinGW, it creates a folder named MinGW in C: Drive. To set the compiler's path, we need the path to the bin directory of MinGW. So, first,

  • Go to C:>MinGW>bin.
  • Now, inside the bin folder, click on the address bar and copy the address.
  • We require this address to be set as the path in the environment variable.
  • If your install location was somewhere else, you may go to that location where you have installed MinGW.

Setup path variable for C compiler

Note: If you open command prompt directly in the bin path, the g++ --version command will work properly, but the command should work on all the directories in the computer. That is the main reason to set the environment path variable.

Step 2: Open Edit System Variables. Navigate to the search bar and type Edit the system environment variables and click on open to continue to edit system environment variables.

Edit System Variable for C

Step 3: Edit the Path. In the User variables for the User section, select the path and click on the Edit button.

Edit the Path for C

Step 4: Setup a New Path.

  • After clicking on the Edit button, a new window, Edit environment variable will open. This window allows us to add the path as per our requirements.
  • Since we want to add a new path, click on the New button. A new window, Edit environment variable, will open. This window allows us to add the path as per our requirements. Click on the ' New ' button since we want to add a new path.

Set up new Path for C

Step 5: Paste the Path. Paste the path of the MinGW bin that was copied earlier and click on Ok.

Paste Path

Creating and Running a C program

Step 1: Hello World in C. To execute a C program, create a text file in any directory of your choice.

Create a Hello World Program in C

Step 2: Type the C code and Save the file. Type the code in the notepad and save the file with the .c extension. Here we write a program to print hello world to demonstrate this step and save the file as Hello.c.

Type Code for C Program

Step 3: Open Command Prompt. Now, click on the address bar in the C program's directory, type cmd, and press Enter.

Open Command Prompt

Step 4: Compile the C program. To compile the Hello World code that we wrote earlier, type gcc Hello.c (or the name by which you will save the program) and press enter. Writing gcc will invoke the C compiler for windows.

Compile the C Program

Step 5: Compilation completed. The compiled file will be saved in the same directory with the name a (the name can be different for you). The type of the file will be Application.

Compilation complete for C program

Step 6: Running the C Program. To run the compiled file, write the name of the compiled file, i.e., a, as shown in the screenshot below. Finally, the output will be printed in the command prompt.

Run the C Program

There are alternate methods to install C Compilers. One of them is by installing Code::Blocks IDE.

Let's see the step-by-step process to install Code::Blocks IDE with C Compiler.

Alternate Method:

Installation using CodeBlocks IDE Binary release

Code::Blocks Code::Blocks is a C/C++ IDE. It comes with plugins which are external additions to any software for customization. Any kind of functionality can be added by installing a plugin. Plugins like debugger, text formatter, etc. can be added to the IDE.

Below are the steps to download and install the Code::Blocks IDE.

Step 1: Go to Code::Blocks Website. The first step is to install the Code blocks IDE. Go to any of the browsers and open codeblocks.org. The below web page will appear on your screen. On its left side, click on the Downloads.

CodeBlocks Website Step 2: Choose the way to download. After clicking on the downloads on the download's page, it will redirect you to the next page. Here click on the

Download the binary release, as we will use binary release for this tutorial.

Download the Binary Release

Step 3: Choose the version for download In the Windows section, click on the codeblocks-20.03mingw-setup.exe (for the 64-bit version).

The codeblocks-20.03mingw-setup.exe comes with the MinGW, including the GCC/G++/GFortan compiler and GDB debugger.

To download the 32-bit version, choose the codeblocks-20.03mingw-32bit-setup.exe

Download the file from the link in front of the selected version, as highlighted in the screenshot below.

Download the Preferred Version

Step 4: Start the installation Now, the installation file has been downloaded. Double-click on the file and the setup window will appear on the screen. Click on the Next> button and proceed further.

Start Code Block Installation

Step 5: Accept the License Agreement The license agreement for terms and conditions will appear on the screen. Read the license agreement, click on the I Agree button, and proceed further.

License Agreement for Code Blocks

Step 6: Choose Components. In this step, check whether all the checkboxes are checked or not. For smooth installation, all components should be selected. After that, click on Next to continue.

Code Blocks Components

Step 7: Choose the install location and start installing. Choose the location on the drive where the application is to be installed. It automatically takes the C: Drive location. To change it, click on browse, choose the location, and click on Next to proceed further.

Start Code Blocks Instllation

The installer will start to extract the files on the destination folder you chose in the previous step, and installation will begin.

Extract Files in Folder

Step 8: Installation complete. The installation is completed successfully. Once the installation is finished, a pop message saying - Do you want to run Code::Blocks now? Will appear. You may click on the Yes button to start the Code::Blocks IDE.

Now make sure the IDE detects the C compiler for Windows. To do that, follow the steps below. Complete Codeblocks Installation

Step 9: Setting up MinGW to PATH.

Go to the Folder of the MinGW on your computer C:\Program Files\CodeBlocks\MinGw\bin. The following folder will open on your computer. Just copy the path on the address bar.

Setting up MinGW to PATH

Now refer to the steps mentioned in the Setting up PATH Variable in the previous section. After setting up the Environment path variable, proceed with the next steps.

Step 10: Compiler auto-detection. As soon as you open up the Code::Blocks IDE, the Compilers auto-detection window will appear with the GNU GCC compiler status shown as Detected.

GNU GCC compiler Auto-Detection

Creating and Running a C Program in Code::Blocks.

Step 1: Open a New File in Code::Blocks.

Select File>>New>>Empty File and click on it to check whether the IDE is working properly.

New File in the Code Block Step 2: Write a Hello World program in C.

Make a hello world program in C. Save it with the .c extension.

Write Hello World program in C

Step 3: Output of C Program. Once you save, go to Build>>Run and the program will compile and run. If the command prompt with hello world written on it appears on the screen below, then the code blocks have been successfully installed!

Output of Hello World Program in C

Conclusion

  • The source code is compiled using a C compiler for Windows to be understood by the machine (computer).
  • The C Compiler for Windows can be installed on a Windows system, first by only installing the compiler and second by installing a compiler with IDE as a plugin.
  • The C/GCC is the compiler used by the C language.
  • We have to set the path of the compiler in the Environment path variable so that it can be used in any directory.
  • To use C Compiler, the program must be saved with .C extension.

See Also: