Creating and Running The First C Program

Creating a new project #

To create and run programs in Code Blocks you first have to create a project.

So what is a project ?

In simplest terms, you can think of a project as a collection different source files. A small project can also have a single source file.


To create a new program we have to first create a project.

1) Go to File > New > Project. A wizard will be presented as shown in the following screenshot.


Select Console application and click Go.

2) A Console Application wizard will be presented. Click on the Next button.


3) In the next window of the Console Application wizard select the language which you want to use in the project.


Select C and click Next.

4) In the next window enter project title as "First App" and choose a path to save the "First App" project.


Click on the Next button to continue.

5) This window allows you select compiler for the project. Select GNU GCC Compiler and keep other settings to default.


Click Finish. A new project will be created for you along with some default code.

Once the project is created, Code Blocks IDE will look something like this:


Double click on the Sources folder to view files under it. Take a look at the Management Window that has been populated with newly created project files.


As you can see at this time the project contains only one file main.c. Double click to open main.c in the editor window.

Let's replace the default with the following code.

#include <stdio.h>

int main()
    printf("My First App");
    return 0;

Note: Do not copy and paste programs, just type, it will be more beneficial for you. We will discuss in detail how the program works in later chapters.

Save the program by pressing Ctrl + S or hitting save icon in the toolbar.


Compile the program by selecting Build > Build from the menu bar or by hitting Ctrl + F9 . If compilation succeeds, you will see some messages on the Build Log tab of the Logs Window.


Notice the last line of the log which says "0 error(s), 0 warning(s) ". It simply means that the program is compiled successfully without any errors and warnings.

Run the program by selecting Build > Run from the menu bar or by hitting Ctrl + F10 . When you run the program, you will see a window like this:


To close this window press any key on the keyboard.

Tip: You can also press F9 or Build > Build and Run to compile and run the program in one step.

Help me! I got an error while compiling #

Compilation errors or Compile time errors occurs when you have made a mistake while typing the program. These typing mistakes are known as Syntax errors. Just like the English language has grammatical rules Computer languages have Syntax rules. In other words, the syntax dictates how a language should be written. For example, one such rule is: Every statement in C must end with a semi-colon(;).

Compiler reports syntax errors in situations such as:

  • Ending a statement without a semicolon(;).
  • Mistyped keyword.
  • There is an opening brace ({) without a closing brace (}).
  • Trying to use an declared variable.

So make sure you have typed the code as it is, with no typos or misspelling.

When a syntax error is encountered by the compiler while compiling the program. It reports a syntax error message. This message contains the line number at which error is found and a description of the error.

The compiler can detect the problems in two levels: warning and error.

Warning: It simply means you are doing something wrong. Although it is syntactically valid but it may cause problems in the future. Code Blocks displays warning messages in blue color. Warnings do not halt the compilation process.

Errors: Error is a fatal flaw in the program. Errors halt the compilation of the program. To compile the program you must first resolve all errors(syntax errors). Code Blocks displays errors in red color.

When a syntax error is encountered the Code Blocks displays wealth of information in the Build message tab. For example: Suppose that by mistake, you have left the semicolon at the end of line 5.

#include <stdio.h>

int main()
    printf("My First App")
    return 0;

Had you compiled this program you would have gotten the following errors.


As you can see in the logs the compiler is reporting an error about missing semicolon in line 6. Although, no doubt errors messages provided by the compiler are useful, they may or may not be very accurate. For this reason, the error reported by the compiler may not reflect the original cause of the problem. For example: In the above program, the compiler is reporting an error at line 6, but we know that the actual problem is in line 5 due to missing semicolon (;). So the whole point of this discussion is that when compiler reports syntax error don't take compiler's message as it is, to find the actual error look around few lines above or below where the error was actually reported.

The errors in your program should be resolved by now, if not comment below and we will try to solve it together.