Your First C Program in Code::Blocks: Projects, Build, and Run

This is the first lesson in the Introduction to C series. It does not begin with a long list of rules. Instead, you open Code::Blocks, get a program running, and then change one small thing at a time and watch what happens. Along the way you will make a couple of mistakes on purpose, because seeing what goes wrong and why is one of the fastest ways to learn. If you are studying CAPE Computer Science Unit 1, or starting C on your own, open Code::Blocks and follow each step.

In this lesson you will

  • Create a Code::Blocks project (not just a file) for a C console program
  • Choose where the project lives on disk, and learn why the folder path should be short
  • Tell the difference between Build, Run and Build and run
  • Read the build log and the “Process returned 0” message
  • Recover when the IDE seems to stop responding

Try it: create your first project

Whenever you write C in Code::Blocks, create a project, not a loose file. A project keeps your source code, your settings and your compiled program together in one folder.

  1. Open Code::Blocks and choose File → New → Project…
  2. Choose Console application, then Go. Click Next on the welcome page of the wizard.
  3. When asked for the language, choose C, not C++. (Some other beginner IDEs, such as Dev-C++, lean towards C++ by default. In this series you write C.)
  4. Give the project a title: 001-FirstCProgram. The number at the front keeps your projects in order (001, 002, 003…) as the series goes on. Capitalising the first letter of each word, as in FirstCProgram, is called upper camel case (also called PascalCase).
  5. Choose a folder to create the project in. Pick one close to the top of your drive, and use a sensible, school-appropriate folder name.
  6. On the next page, leave the compiler as GNU GCC Compiler with both Debug and Release ticked. If you installed Code::Blocks with MinGW, this is already filled in for you. Click Finish.

A good example of a short project location:

C:\CPrograms\2026-2027\001-FirstCProgram

Why keep the path short? Windows (and some tools) can run into trouble when a full file path is longer than about 255 characters. The same problem can stop a web browser from uploading an assignment buried deep inside many folders. If a project sits inside folder after folder, you may later see strange compile or debug errors. A short path avoids that.

In the Projects panel on the left, expand Sources and double-click main.c. It opens in a tab, and Code::Blocks has already written a first program for you:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

Try it: build, then run

Find the build toolbar near the top of the window. For now, use the two buttons separately so that you can see what each one does:

  • Build (the gear icon) turns your source code into a program the computer can run.
  • Run (the green play arrow) starts the program that was last built.
  • Build and run (gear plus arrow) does both, one after the other.

Click Build and look at the Build log at the bottom of the screen. You should see a line ending in 0 error(s), 0 warning(s), along with how long the build took. Now click Run. A console window opens:

Hello world!

Process returned 0 (0x0)   execution time : 0.012 s
Press any key to continue.

The “Process returned 0” line comes from return 0; at the end of main. By convention, a program that finishes normally returns 0. (Your execution time will be different.) Press a key to close the window.

Now change one thing. Show that you are in charge of this code by changing the message to a name. Use your own name, or Ken as in this example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello, my name is Ken\n");
    return 0;
}

Click Build and run. The console now shows:

Hello, my name is Ken

Tip: hold Ctrl and turn the mouse wheel to make the code larger or smaller. This works in many programs, including word processors.

Break it on purpose: press Run without building

Close the console window. Change the name again, so that the printf line reads printf("Hello, my name is Ryu\n");. This time click only Run (the green arrow), not Build.

Observe: the console still shows the old message:

Hello, my name is Ken

Your edit seems to have been ignored. Now click Build, then Run (or simply Build and run):

Hello, my name is Ryu

A second trap to try: leave the console window open, go back to the editor, and try to build again. The build buttons are greyed out. Code::Blocks will not rebuild a program that is still running. Close the console (press a key in it), and the buttons come back. If a console window ever refuses to close, you may need to end it, or Code::Blocks itself, from Task Manager.

What just happened

The text you type in main.c is source code. The computer cannot run it directly. When you click Build, Code::Blocks asks the GCC compiler to translate your source code into an intermediate object file (obj/Debug/main.o), and then to make a finished executable (bin/Debug/001-FirstCProgram.exe on Windows). Clicking Run simply starts whatever executable is already in that folder.

So when you edited the code and pressed only Run, you started the old executable, which was built before your change. The new source code had not been compiled yet. The habit to build: after every change, build again. Build often and test small changes one at a time. When something breaks, you will know exactly which change caused it.

This is the job of an integrated development environment (IDE). It puts an editor, a compiler, a build log, a way to run your program and (later in the series) a debugger together in one window.

No Code::Blocks right now? If GCC is installed, you can do the same two steps from a terminal. gcc main.c -o hello is the build step, and ./hello (or hello.exe on Windows) is the run step. Edit the file and run ./hello again without rebuilding, and you will see the old message, exactly like the experiment above.

Key terms

  • Project: a folder plus a Code::Blocks project file (.cbp) that keeps your source files and build settings together.
  • Source code: the human-readable C you type, saved in files ending in .c.
  • Compiler: the program (here, GCC) that translates source code into machine code.
  • Object file: the compiler’s intermediate output (.o), which is then turned into an executable.
  • Build: compile your source and produce an executable.
  • Executable: the finished program file the computer can run.
  • Run: start the most recently built executable. It does not rebuild.
  • IDE: an integrated development environment such as Code::Blocks.
  • Upper camel case (PascalCase): capitalising the first letter of every word, as in FirstCProgram.

Quick check

  1. You change your printf message and click Run, but the old message appears. Why, and what should you click instead?

    Show answer

    Run starts the executable that was last built, and your change has not been compiled yet. Click Build and then Run, or simply Build and run.

  2. Why might a project saved deep inside many nested folders cause problems?

    Show answer

    Very long file paths (over about 255 characters) can make tools fail, which shows up as confusing compile, debug or upload errors. Keep project paths short.

  3. The build buttons in Code::Blocks are greyed out. What is the most likely reason?

    Show answer

    Your program is still running in a console window. Close it, and the buttons become available again.

  4. What does “Process returned 0” tell you?

    Show answer

    The program reached return 0; in main and finished normally.

Optional: a recorded demonstration of these steps starts around 4:20. Creating the project begins at about 8:00, and building and running at about 15:30.

Part of the Introduction to C series. Next: printf, String Literals, and Escape Sequences