Lesson 3: First Program

I know that you are ready and excited to write your first program. So I wont late you. Start your Code::Blocks IDE. You will see the IDE like this. [[ Click on the image to view larger ]]

From the File > New menu click on new empty file. Or, you can press CTRL + SHIFT + N all together.

You will be able to see a new Text Editor like this.

This is the place where you will write down your source codes (Programming codes are also called source codes). We need to save the file at first. Remember whenever you open a new empty file at first you should save the file. Press CTRL + S together from your keyboard to see the Save File window.

As we are writing the first program give the name first_program.c . Don’t forget to put extension (.c) after the file name.

Now the file is saved. We are going to write down our first program to print / show Hello World on the screen. Actually it is tradition to start program with this statement. Don’t wait write down the following codes on the editor.

#include <stdio.h>

int main()
    printf("Hello World!");
    return 0;

So you have written your first program! Congrats! If you have written successfully your coding will be looked like this:

Now we have written our first code. But this wont understand your computer. So we need to compile the program to machine language. To do so, from the Build menu click on Compile current file.

Your source file will be compiled into machine language now. If there is no mistake in the program you will see like this on the bottom of the screen.

It means your program is successfully compiled. Now we will Run the program. To do so, from the Build menu click on Run. You will see a black window like this. It is called console (Command Prompt in windows).

The first line of the window is Hello World. Congratulations to you. You have successfully compiled and run your first program!

Now you may want to know the explanation of what you did so far. Well in the first line of the program we have written is #include <stdio.h>.

In C, there are many functions are built in. You don’t need to write down codes for those functions. That built in functions are included in some files called header files. Here stdio.h is one of the header files in C and .h is the extension of the file. Stdio stands for Standard Input Output. All input and output related functions are included in stdio.h.

The second line is empty. I have done this because it looks fine. Actually it is a good practice to do that.

Now come to the third line. Here we see int main(). Every C program consists of one or more function. But there must be at least one function in C. And that is called main function. You will know more about functions later.  Here int is stand for Integer and it is a data type in C.

The fourth line has a starting curly brace ( { ). Your main program will be inside a pair of curly brace. Actually your compiler will compile the texts inside the curly brace of main function.

Now look at the fifth line. I have written codes after four spaces. You should give these spaces because it is a good practice. And your source code will look neat and clean. After four spaces there is printf(“Hello World!”);  Here printf() is another function. But it’s a built in function in C. And it’s a standard output function. That’s why we have included stdio.h file at the beginning of the program.

What does actually printf do? Everything inside a pair of double quotation (“ “) is printed on the screen. We have  written Hello World!, so it is printed on the screen. And then there is a semicolon (;). In C, every statement must be ended with a semicolon. This is the syntax (rule) of c programming. Never forget to put semicolons at the end of each statements.

Now remove the semicolon from your program. Now try to compile your program. Your program wont compile. Rather, you will see an error like this:

Now put back the semicolon. Your program will be compiled successfully now.

Now lets talk about the seventh line. It only contains return 0; . As your  main functions return type is int, it will return an integer. And this integer is 0 here. You will learn more about return type later in Functions. Now just keep in mind that you must put return 0; before ending any program.

And the last line is the ending curly brace ( } ). And with this your first program is completed.

I hope you have understood the program. Now I am giving you a home task. Just write down a program that will print “I can do programming!” on the screen. You will see my next lesson soon. To get lessons in you inbox follow this blog by email.

If you have any questions just ask me in the comments. Thank you.


Spam comments will be deleted. :)