0
0
CHow-ToBeginner · 4 min read

How to Install GCC on Windows for C Programming

To install gcc on Windows for C programming, download and install MinGW-w64 or MSYS2, which provide the GCC compiler. After installation, add the compiler's bin folder to your system PATH environment variable to use gcc from the command line.
📐

Syntax

The basic command to compile a C program using gcc is:

gcc [options] source_file.c -o output_executable

Explanation:

  • gcc: The GNU C Compiler command.
  • [options]: Optional flags like -Wall for warnings.
  • source_file.c: Your C source code file.
  • -o output_executable: Specifies the name of the compiled program.
bash
gcc [options] source_file.c -o output_executable
💻

Example

This example shows how to compile and run a simple C program after installing gcc on Windows.

c
#include <stdio.h>

int main() {
    printf("Hello, GCC on Windows!\n");
    return 0;
}
Output
Hello, GCC on Windows!
⚠️

Common Pitfalls

Common mistakes when installing or using gcc on Windows include:

  • Not adding the bin folder of MinGW or MSYS2 to the system PATH, causing gcc to be unrecognized.
  • Downloading incomplete or wrong versions of MinGW that lack gcc.
  • Confusing 32-bit and 64-bit versions leading to compatibility issues.
  • Running gcc commands in a regular Command Prompt without the environment set up properly.

Always verify installation by running gcc --version in a new terminal window.

bash
REM Wrong: Not in PATH
> gcc --version
'gcc' is not recognized as an internal or external command

REM Right: After adding to PATH
> gcc --version
gcc (MinGW-W64) 12.2.0
...
Output
'gcc' is not recognized as an internal or external command gcc (MinGW-W64) 12.2.0 ...
📊

Quick Reference

StepDescription
Download MinGW-w64 or MSYS2Get the installer from official websites.
Run the installerFollow prompts to install GCC compiler.
Add bin folder to PATHAllows running gcc from any terminal.
Open new terminalRefresh environment variables.
Test with gcc --versionVerify successful installation.

Key Takeaways

Install MinGW-w64 or MSYS2 to get GCC on Windows.
Add the compiler's bin directory to your system PATH environment variable.
Use the command 'gcc source_file.c -o output' to compile C programs.
Verify installation by running 'gcc --version' in a new terminal.
Avoid mixing 32-bit and 64-bit versions to prevent compatibility issues.