🚀 C# ile Programlamaya Giriş: Kodlamaya İlk Adımını At, Kendi Projelerini Geliştirmeye Başla!
00HOME
07GLOSSARY 08TAGS ♡MY LIST ⌕SEARCH
TRENG

Setup: compiler, editor and your first build

Install the tools you need to write C (GCC/MinGW, Code::Blocks or VS Code) and get to know the four build steps that turn source code into a running program.

Most of the software running on a washing machine board, an energy meter or a motor driver is written in C. The reason is simple: C produces small, fast programs that sit very close to the machine code the processor understands. It lets you manage memory and individual bits directly. In this course we will learn C on your computer first, then carry the same knowledge over to microcontrollers. The first job is to install the tools that turn code into a working program.

What do you need?

C code is a plain text file (with the .c extension). The computer cannot run that text directly; you need a compiler that translates it into machine code. In this course we will use GCC, the most widely used open source C compiler. Add an editor to write code comfortably.

  • Windows: The easiest route is the Code::Blocks installer whose name contains "mingw-setup"; it ships with MinGW (GCC for Windows). If you want a more recent GCC, install MSYS2 and run pacman -S mingw-w64-ucrt-x86_64-gcc in its terminal. In that case, don't forget to add the compiler's bin folder to the PATH environment variable.
  • Linux: On Debian/Ubuntu based distributions, sudo apt install build-essential is enough.
  • macOS: xcode-select --install installs the command line tools. Here the gcc command actually runs the Clang compiler; for this course it makes no difference.
  • Editor: Code::Blocks comes with its own editor. If you like VS Code, install Microsoft's "C/C++" extension; GCC still does the compiling.

After installing, open a terminal (Command Prompt or PowerShell on Windows) and type:

gcc --version

If you see version information, you are ready. If you get something like "command not found", the compiler is not on your PATH.

Build and run your first program

Create a file called merhaba.c in an empty folder and type this into it:

#include <stdio.h>

int main(void) {
    printf("Merhaba, Byte Akademi!\n");
    printf("Ilk C programim calisti.\n");
    return 0;
}

Now go to that folder in the terminal and compile:

gcc merhaba.c -o merhaba
./merhaba
Merhaba, Byte Akademi!
Ilk C programim calisti.

-o merhaba sets the name of the output file. On Windows the file becomes merhaba.exe; you run it with .\merhaba in PowerShell or just merhaba in Command Prompt. We will go through what the code means line by line in the next lesson.

Building from the editor: Code::Blocks and VS Code

Knowing the terminal commands matters, but you don't have to type them every time. Editors run the same command for you.

  • Code::Blocks: Choose "C/C++ source" from File > New > File, select C as the language and save the file. F9 (Build and run) compiles first and then runs the program in a separate console window. The bottom of that window also shows the program's return value and run time. If you get a warning that the compiler cannot be found, point to the MinGW folder under Settings > Compiler > Toolchain executables.
  • VS Code: Once the file is open, the "Run C/C++ File" button offered by the extension, or the Terminal > Run Build Task menu, calls GCC. The first time it may ask you to pick a compiler; choose gcc from the list. The settings are stored in a tasks.json file inside the .vscode folder. If you add the -Wall and -Wextra flags there, warnings will show up on every build.

Which editor you pick does not matter. What matters is knowing that the same gcc command runs in the background. When something goes wrong, you can see the full command and the error message in the editor's "Build log" or "Terminal" panel. In this course we will give commands in terminal form; you only need to set up the same thing once in your editor.

Behind the scenes: the four build steps

gcc looks like a single command, but it does four jobs in the background. Knowing them helps you understand which stage an error message comes from:

  1. Preprocessing: Lines starting with #, such as #include and #define, are handled. The contents of stdio.h are pasted into your code. To see it: gcc -E merhaba.c
  2. Compiling: The C code is translated into assembly language for your processor. To see it: gcc -S merhaba.c (the result is merhaba.s).
  3. Assembling: The assembly becomes an object file containing machine code: gcc -c merhaba.c (the result is merhaba.o).
  4. Linking: The object file is combined with library functions such as printf, producing an executable file.

The same chain runs for a microcontroller. The difference is that the end result is a file the controller understands (for example a .hex file), which is then flashed onto the board.

Keep warnings on

The compiler can report not only errors but also situations where it thinks "this works, but it is probably wrong". To get those, always compile with these flags:

gcc -Wall -Wextra -std=c11 merhaba.c -o merhaba

-Wall and -Wextra turn on warnings, and -std=c11 tells GCC to follow the C11 standard. Code that compiles without warnings will be our goal for the rest of the course.

Common mistakes

Saving the file with a .cpp extension: Some editors open a C++ file by default. In a C course your file must end with .c; otherwise C++ rules kick in and some examples behave differently.

  • Running without saving or rebuilding: If you changed the code but are still running the old .exe, you won't see the change. Save and rebuild after every edit.
  • Skipping the PATH setup: A "gcc is not recognized" error almost always means the compiler's bin folder was not added to PATH. After adding it, close and reopen the terminal.

Exercise

Write a program that prints two lines: your name on the first line and "5 V / 1 kOhm = 5 mA" on the second. Compile it with the warning flags and name the output file ohm.

Hint: copy merhaba.c and change only the text inside the quotes. Use -o ohm in the build command.

Solution

#include <stdio.h>

int main(void) {
    printf("Ayse Yilmaz\n");
    printf("5 V / 1 kOhm = 5 mA\n");
    return 0;
}
gcc -Wall -Wextra -std=c11 ohm.c -o ohm
./ohm
Ayse Yilmaz
5 V / 1 kOhm = 5 mA

Your compiler now works and you know how code turns into machine code. In the next lesson we will open up our first program line by line and see what we can do with printf.

// Spot the bug +10 XP

Exactly one line below is wrong. Click the faulty line.

// Mini quiz

Click an option; you will see right away, with the reason, whether it is correct or not.

1. Which GCC flag runs only the preprocessing step so you can see its result?
2. What does "-o merhaba" do in the command "gcc merhaba.c -o merhaba"?
3. Which step of the build chain adds library functions such as printf to the program?

Mark it when you finish: your progress is saved and you earn +20 XP.

Tip: ← → keys let you move between lessons.