Langage C #2 - compiler programme
Table of Contents
Introduction
This tutorial will guide you through the process of compiling your first C program using the GCC compiler. It is essential for turning your source code into an executable program, which is a critical step in software development.
Step 1: Install MinGW
Before you can start compiling your C programs, you need to install MinGW, which includes the GCC compiler.
- Visit the MinGW installation guide by following this link: Install MinGW.
- Download the MinGW installer from the official site.
- Run the installer and follow the prompts:
- Select the components you want to install. Ensure that you include the GCC compiler.
- Choose the installation directory (e.g., C:\MinGW).
- Once installed, add MinGW to your system path:
- Open the Control Panel and navigate to System and Security > System.
- Click on "Advanced system settings."
- In the System Properties window, click on the "Environment Variables" button.
- Find the "Path" variable in the "System variables" section and click "Edit."
- Add the path to the MinGW bin directory (e.g., C:\MinGW\bin).
- Restart your command prompt to apply the changes.
Step 2: Write Your C Program
Now that you have installed MinGW, it’s time to write your first C program.
- Open a text editor (e.g., Notepad, Sublime Text, or VS Code).
- Write a simple C program. For example, you can create a file named
hello.c
with the following code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
- Save the file in a directory where you can easily find it (e.g., C:\CPrograms).
Step 3: Compile the C Program
With your program written, you can now compile it using the GCC compiler.
-
Open the Command Prompt (cmd).
-
Navigate to the directory where you saved your C program. For example:
cd C:\CPrograms
-
Compile the program using the following command:
gcc hello.c -o hello.exe
hello.c
is your source file.-o hello.exe
specifies the output filename.
-
If there are no errors, the command prompt will return to the input line without any messages.
Step 4: Run Your Compiled Program
Now that you have compiled your program, you can run it.
- In the command prompt, type the following command:
hello.exe
- You should see the output:
Hello, World!
Conclusion
You have successfully installed MinGW, written a simple C program, compiled it using GCC, and run the executable. This process is fundamental for any C programming tasks you will undertake.
Next Steps
- Experiment with different C programs to deepen your understanding.
- Explore other functionalities of GCC, such as debugging options and optimization flags.
- Consider learning about integrated development environments (IDEs) to streamline your programming workflow.