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.
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
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.
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
On Windows, GCC can be used through toolchains such as MinGW-w64. These provide GCC and other development tools needed to compile C programs.
Many Linux distributions provide GCC through their package manager. After installation, the compiler can be used from the terminal.
gcc --version
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.
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.
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 |
The general installation process is:
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.
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.
Windows provides Command Prompt, which can be used to run compiler commands. After opening Command Prompt, you can check GCC with:
gcc --version
Linux users commonly use the Terminal to compile and run C programs. For example:
gcc program.c -o program
macOS users can use Terminal for C development. After installing the required development tools, compiler commands can be executed from the Terminal.
C source files normally use the .c extension. For example:
hello.c
The file can contain a C program that will later be compiled.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Save the program as hello.c.
Use the GCC compiler to compile the source file:
gcc hello.c -o hello
The -o option specifies the name of the output program.
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
On Linux, an executable created with GCC can commonly be run using:
./hello
A simplified C compilation process can be represented as:
Source Code
↓
Preprocessing
↓
Compilation
↓
Assembly
↓
Linking
↓
Executable Program
The preprocessor handles instructions that begin with #, such as
#include and macros, before the main compilation stage.
#include <stdio.h>
During compilation, the C source program is translated into lower-level code that can be processed further toward an executable program.
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.
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.
Make sure the C source file has the .c extension.
Correct: hello.c
Wrong: hello.txt
Popular C compiler choices include:
The commands and installation process can differ between compilers.
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.
The easiest way to test a basic GCC installation is:
gcc --version
Then create a small C program, compile it, and run it.
.c file.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.
.c extension.gcc --version can be used to check GCC.gcc hello.c -o hello compiles a basic program.Question: Which software translates C source code into a form that can be executed by the computer?