Lesson 5 of 60 – Installing C Compiler
8%

Installing C Compiler

Before writing and running C programs, we need a C compiler. A compiler converts C source code into machine code that the computer can execute. In this lesson, we will learn what a compiler is and how to install a C compiler on a computer.

Note: GCC is one of the most commonly used C compilers. It is available for Windows, Linux, and macOS.

1. What is a Compiler?

A compiler is a software program that converts source code written in a programming language into a form that the computer can execute.

C Source Code → Compiler → Machine Code

2. Why Do We Need a C Compiler?

Computers do not directly understand C source code. The C compiler translates the C program into machine instructions.

Without a compiler or suitable toolchain, a C source file cannot normally be built into an executable program.

3. What is GCC?

GCC stands for GNU Compiler Collection. It contains compilers for several programming languages and is widely used for compiling C programs.

gcc program.c -o program

4. GCC on Windows

On Windows, GCC can be used through toolchains such as MinGW-w64. These provide GCC and other development tools needed to compile C programs.

5. GCC on Linux

Many Linux distributions provide GCC through their package manager. After installation, the compiler can be used from the terminal.

gcc --version

6. GCC on macOS

On macOS, C development tools can be installed using Apple's command-line development tools. A C compiler can then be accessed from the Terminal.

7. What is an IDE?

An IDE means Integrated Development Environment. It provides tools for writing, editing, compiling, and debugging programs.

An IDE can make learning C easier because the editor and compiler can be used from one application.

8. Compiler vs IDE

A compiler translates source code, while an IDE provides a complete development environment around tools such as a compiler and debugger.

Compiler IDE
Compiles source code Provides an editor and development tools
Usually works from command line or build tools Can provide a graphical interface

9. Installing a C Compiler

The general installation process is:

  1. Choose a C compiler or development toolchain.
  2. Download it from a trusted source.
  3. Install the required components.
  4. Configure the system if required.
  5. Open a terminal or command prompt.
  6. Check the compiler version.

10. Check Whether GCC is Installed

After installing GCC, open a terminal or command prompt and type:

gcc --version

If GCC is correctly installed and available in the system path, its version information will be displayed.

11. What is PATH?

The PATH is an environment variable that contains locations where the operating system looks for executable programs.

If the compiler's executable directory is included in PATH, commands such as gcc can usually be run directly from the terminal.

12. Command Prompt on Windows

Windows provides Command Prompt, which can be used to run compiler commands. After opening Command Prompt, you can check GCC with:

gcc --version

13. Terminal on Linux

Linux users commonly use the Terminal to compile and run C programs. For example:

gcc program.c -o program

14. Terminal on macOS

macOS users can use Terminal for C development. After installing the required development tools, compiler commands can be executed from the Terminal.

15. Create a C File

C source files normally use the .c extension. For example:

hello.c

The file can contain a C program that will later be compiled.

16. Write a Simple C Program

#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

Save the program as hello.c.

17. Compile the C Program

Use the GCC compiler to compile the source file:

gcc hello.c -o hello

The -o option specifies the name of the output program.

18. Run the Program on Windows

After compiling the program on Windows, the generated executable can be started from the command prompt.

hello

Depending on the toolchain and shell, the executable may also be run as:

.\hello.exe

19. Run the Program on Linux

On Linux, an executable created with GCC can commonly be run using:

./hello

20. Compilation Process

A simplified C compilation process can be represented as:

Source Code
     ↓
Preprocessing
     ↓
Compilation
     ↓
Assembly
     ↓
Linking
     ↓
Executable Program

21. What is Preprocessing?

The preprocessor handles instructions that begin with #, such as #include and macros, before the main compilation stage.

#include <stdio.h>

22. What is Compilation?

During compilation, the C source program is translated into lower-level code that can be processed further toward an executable program.

23. What is Linking?

The linker combines the required object code and libraries to create the final executable program.

For example, functions from the C standard library can be linked into a program.

24. Common Compiler Error

If the terminal shows an error such as:

'gcc' is not recognized

the compiler may not be installed correctly, or its executable directory may not be available through the system PATH.

25. File Extension Mistake

Make sure the C source file has the .c extension.

Correct: hello.c
Wrong:   hello.txt

26. Choosing a Compiler

Popular C compiler choices include:

  • GCC
  • Clang
  • Microsoft C/C++ compiler

The commands and installation process can differ between compilers.

27. Choosing an Editor

A C program can be written in a simple text editor or a programming editor. The important point is that the file should contain valid C source code and be saved with the .c extension.

28. Test Your Installation

The easiest way to test a basic GCC installation is:

gcc --version

Then create a small C program, compile it, and run it.

29. Basic Installation Checklist

  • Install a C compiler.
  • Install an editor or IDE if needed.
  • Make sure the compiler command is available.
  • Check the compiler version.
  • Create a .c file.
  • Compile the program.
  • Run the executable.

30. Ready to Learn C

Once the compiler is installed and a simple program runs successfully, your C programming environment is ready.

In the next lesson, we will write and understand our first complete C program.

📌 Key Points

  • A compiler converts C source code into executable machine-level code.
  • GCC is a widely used C compiler.
  • C source files normally use the .c extension.
  • The command gcc --version can be used to check GCC.
  • The command gcc hello.c -o hello compiles a basic program.
  • PATH allows compiler commands to be found from the terminal.
  • After installation, always test the compiler with a simple program.

🧠 Quick Quiz

Question: Which software translates C source code into a form that can be executed by the computer?