Ryan's Code Projects

Blog and showcase of Ryan Pridgeon

Part 1: Getting Started With C Programming

>> CLICK HERE TO GO TO PART 2: BASIC VARIABLES >>

Hello, and welcome to my first tutorial. In this tutorial, I will help you set up a programming environment, and guide you through to writing and running your first C program.

The program we will be using to write and compile our programs in this tutorial is called Code::Blocks. It’s a very lightweight, easy to use IDE ( Integrated Development Environment ) and will be perfect to set you on your way into C programming.

Step 1: Installing & Setting Up the Program

First, you will need to download the Code::Blocks IDE. Follow this link to download the installer;

http://downloads.sourceforge.net/codeblocks/codeblocks-8.02mingw-setup.exe

When you have downloaded the installer, start it up and follow the steps. I recommend you install it to C:/Codeblocks, rather than in Program Files, because this makes life easier when you become better at programming. However it is totally your choice.

Follow the install through until you’ve succesfully installed the program. Now that it is installed, open it up and you should be presented with a screen that looks something like this;

Make sure you select GCC as your default compiler.

Click “GNU GCC Compiler” (usually right at the top of the list) , and click Set as Default. Be warned that you do not click any of the other GCC compilers (such as GNU GCC Compiler for MSP430).

Now that you’ve set a default compiler, let me tell you what a compiler does.

When we write programming code, all we’re doing is writing into a text file. As it is, the code cannot be run by our computers. This is why we use a Compiler, which takes our code, and turns it into an executable (.exe) file, which our computer can run.

Step 2: Creating a New Project

Now that you should be able to run Codeblocks, you are ready to start a new project.

When we work with C, we use ‘projects’ to manage our files. Each project makes a program from the code in the project.

Because we want to make a new program today, we’re going to click Create New Project.

Click on New Project in the Codeblocks start screen.

Click on New Project in the Codeblocks start screen.

Now we are presented with a choice of project templates to help us get started.

However we only want to make a basic starting program. So we choose Empty Project, and click Go!

emptyproject

After clicking Go, we are presented with a setup guide. Click Next and you will be asked to enter a project name and directory.

For the directory, choose somewhere on your computer where you want to save your programming projects. I’ve use My Documents / Projects as a folder to store all my projects.

Next we enter a name for the project. For mine, I’ve called it ‘First’. You can choose whatever name you want.

(You do not have to change Resulting Filename and Project Filename fields, so leave those as they are.)

filenames

Now click next and make sure your screen looks like this;

config

Now click Finish and you will have created your first Codeblocks Project!

Congratulations.

Now we’re going to add a source file for our project. This will contain our code. To do this, we first click File -> New -> Empty File.

newfile

Then Codeblocks will ask us if we want to add the file to our project. Click YES.

Now save the file as “main.c”   . It is important you give it this name, so that the compiler knows this is the main code.

Make sure we are adding the file to the right compile targets and click OK.

targets

Now we finally get to write some C!

The first thing we are going to write in our new file is

#include <stdio.h>

This first line of code is basically saying “Allow us to use the Standard Input/Output methods.”

This allows us to write to the screen, and also allow the user to type values into our program.

Our next line of code will be

int main(){

This signals the start of our program (our ‘main’ procedure). The code after this will be what the computer runs when we start our program.

The int stands for integer. You do not need to know much about this yet, but it mainly allows us to give error numbers if something goes wrong.

In this program, we will write the words “Hello, World!” onto the screen. To do this we use “printf”. This is our next line of code

printf(“Hello, World!”);

‘printf’ is what we call a ‘function’ which prints a string onto the screen. A string is basically text – a string of seperate characters (letters) next to each other. What printf does is basically put a string (some text) onto our screen.

After writing onto the screen, we want to end our program. We now write the line

return 0;

This gives the error code 0. 0 means there have been no errors during our program. This basically tells the computer that the program has finished without any problems.

Our final line will contain

}

This closes our “main” function. This tells the computer to stop running our code here.

Now your code is complete! Your final code should look like this;

#include <stdio.h>

int main(){

printf(“Hello, World!”);

return 0;

}

#include <stdio.h>
int main(){
puts(“Hello, World!”);
return 0;

When you’re done, press F9 to compile and run your program!

You should be presented with the following screen;

run

Congratulations! You’ve created your first C program.

I hope this leads you on a long, fun, and interesting journey in C programming.

Thankyou for reading this tutorial.

>> CLICK HERE TO GO TO PART 2: BASIC VARIABLES >>

-Ryan


Categorised as: Tutorials


2 Comments

  1. pixelp says:

    Nice tutorial, should help beginners out!

  2. Hi, I love your site http://ryanpridgeon.net but when I post a comment I get a 403 error sometimes. Thought you may want to know

Leave a Reply

Your email address will not be published. Required fields are marked *