Introduction: How to Download Code Blocks and Write a Hello World Program in C++

Fernando Soler-Casanova, Alberto Young, Jacob McPeek and Sterling Neilly

Step 1:

In your web browser, go to the website: www.codeblocks.org

Step 2:

Click on the Downloads Tab on the toolbar

Step 3:

You are now given 3 options for downloading Code::Blocks, select the “download the binary release” option.

Step 4: Installation Instructions for Mac (Windows Users Skip to Step 19)

On the next page, you are given the option to select which codeblocks setup package you wish to download based on your operating system (Windows, Linux 32-bit, Linux 64-bit, Mac). Click on the link for Mac OS X given at the top of the page.

Step 5:

Once you have clicked on the Mac OS X link, you will see table with three titles (File, Date, and Download from), click on the first link under the “Download from” title. This will redirect you to a website that will automatically begin downloading the Code::Blocks installation file onto your computer.

Step 6:

After a few seconds, a zip file containing the Code::Blocks application should appear to begin downloading at the bottom of your web browser, once the download is complete, simply click on the file at the bottom of your web browser.

Step 7:

After clicking the file, a new window should pop up, which is the finder on mac, with the code blocks application present, double click on the Code::Blocks application to launch it.

Step 8:

After double clicking on the application, Code::Blocks will launch and take you to the applications home screen.

Step 9:

In the middle of the applications home screen, you will see a link titled “create a new project”, click on this.

Step 10:

A new window should now appear, which gives you many options for what type of application/project you would like to begin creating, select the “console application” option and click on the “Go” button on the top right side of this window.

Step 11:

Next you will be taken through a setup wizard, which will simply ask you to configure your project. The first step of the setup wizard is an introduction to the setup wizard; just click the “Next” button located at the bottom of this window to proceed to the next step.

Step 12:

The next step will ask what programming language you wish to use for your new project, you will be given the option to choose either C or C++, for our purposes, select “C++”, and click the “Next” button located at the bottom of this window.

Step 13:

The next step will ask you to create a title for your project and to specify a folder where your project will be stored once you save it. To create a title for your project, simply type in a name in the first text box titled “Project title”. (Note: Avoid titles with spaces as well as symbols, if you wish to space out something use an underscore ( _ ) instead of a space). Once you create a title, you will notice the third and fourth text field will automatically fill out, so you don’t have to worry about those.

Step 14:

Next you will select where you wish your project to be saved, so that you know where to access it at a later time. This can be done by clicking on the small square button at the end of the text field titled “Folder to create project in”. A new window should appear after clicking the small square button that allows you to select a folder on your computer you wish to have your project created in. Simply select the desired folder, and click the “Choose” button located at the bottom right corner of the current window. Once you have done this, you will be taken back to the previous window, you can now press the “Next” button located at the bottom of this window.

Step 15:

The next step will ask you to configure the compiler you wish to use, for our purposes, the default selection are perfect, so there is no need to change anything on this step. Press the “Finish” button located at the bottom of this window, to complete the setup wizard.

Step 16:

Now you are almost ready to begin writing code! At the left side of the Code::Blocks window, you will notice a rectangular section titled management, which has various tabs. If it isn’t already selected, click on the projects tab to view your projects file path. Under the “Projects” tab, will see a category titled “Workspace”, which has a tiny arrow to the left of it. Click on this arrow to either show or hide all of this categories content.

Step 17:

You will now see below the “Workspace” category there is a sub category with the name you chose for your project, this is your project and all of its contents.

Step 18:

Under your project’s title, you will see a folder called “Sources”, this is where the files containing the code you will be writing are found. If it isn’t already showing, click the small triangle to the left of the “Sources” folder to show its contents. You will now see a file titled “main.cpp” under the Sources folder, this is the file your code will be written in. Double click on “main.cpp” to open it. This will open your code at the large text area to the right of the “main.cpp” file you just clicked. You are ready to begin coding!

Step 19: Installation Instructions for Windows (Mac Users Skip to Step 38

On the next page, you are given the option to select which Code::Blocks setup package you wish to download based on your operating system (Windows, Linux 32-bit, Linux 64-bit, Mac). Click on the link for “Windows XP / Vista / 7 / 8.x / 10” given at the top of the page.

Step 20:

Once you have clicked on the “Windows XP / Vista / 7 / 8.x / 10” link, you will see a table with three titles (File, Date, and Download from). It is important to download the version titled “codeblocks-16.01mingw-setup.exe”, Code::Blocks will not run properly on the other versions. Click on the link under the “Download from” that corresponds to this version. This will redirect you to a website that will automatically begin downloading the Code::Blocks installation file onto your computer.

Step 21:

After a few seconds, a file containing the Code::Blocks application should appear to begin downloading at the bottom of your web browser, once the download is complete, simply click on the file at the bottom of your web browser. After clicking the file, a window will appear. This is the primary setup wizard, which will guide you through installing Code::Blocks. Click the “Next” button located at the bottom of this window to proceed with the setup.

Step 22:

The next step will ask you to agree to the terms and conditions of Code::Blocks. Once you have read the agreement, click on the “I Agree” button located at the bottom of the page to continue with the setup.

Step 23:

The next step will ask you to choose the components of Code::Blocks you wish to install, the default settings are fine, you there is no need to change anything here, Click the “Next” button at the bottom of the screen to continue.

Step 24:

The next step will ask where you want Code::Blocks to install on your computer, the default settings are fine, you there is no need to change anything here, Click the “Install” button at the bottom of the screen to continue.

Step 25:

Once you click the “Install” button, Code::Blocks will begin installing onto your computer. When the installation is complete, a window will appear asking if you want to run Code::Blocks now. Select “Yes” to Launch Code::Blocks.

Step 26:

Once the Code::Blocks application window appears, you will be at the Code::Blocks home screen. In the middle of the home screen, you will see a link titled “create a new project”, click on this.

Step 27:

A new window should now appear, which gives you many options for what type of application/project you would like to begin creating, select the “console application” option and click on the “Go” button on the top right side of this window.

Step 28:

Next you will be taken through a setup wizard, which will simply ask you to configure your project. The first step of the setup wizard is simply an introduction to the setup wizard; just click the “Next” button located at the bottom of this window to proceed to the next step.

Step 29:

The next step will ask what programming language you wish to use for your new project, you will be given the option to choose either C or C++, for our purposes, select “C++”, and click the “Next” button located at the bottom of this window.

Step 30:

The next step will ask you to create a title for your project and to specify a folder where your project will be stored once you save it. To create a title for your project, simply type in a name in the first text box titled “Project title”. (Note: Avoid titles with spaces as well as symbols, if you wish to space out something use an underscore ( _ ) instead of a space). Once you create a title, you will notice the third text field will automatically fill out, so disregard that one.

Step 31:

Next you will select where you wish your project to be saved, so that you know where to access it at a later time. This can be done by clicking on the small square button at the end of the text field titled “Folder to create project in”. A new window should appear after clicking the small square button that allows you to select a folder on your computer you wish to have your project created in. Simply select the desired folder, and click the “Choose” button located at the bottom right corner of the current window. Once you have done this, you will be taken back to the previous window, you can now press the “Next” button located at the bottom of this window.

Step 32:

Once you have chosen a folder to create your project in, you will notice the fourth text field will automatically fill out, so disregard that one. The next step will ask you to configure the compiler you wish to use, for our purposes, the default selection are perfect, so there is no need to change anything on this step. Press the “Finish” button located at the bottom of this window, to complete the setup wizard.

Step 33:

Now you are almost ready to begin writing code! At the left side of the Code::Blocks window, you will notice a rectangular section titled “Management”, which has various tabs. If it isn’t already selected, click on the projects tab to view your projects file path.

Step 34:

Under the “Projects” tab, will see a category titled “Workspace”, which has a tiny arrow to the left of it. Click on this arrow to either show or hide all of this categories content.

Step 35:

You will now see below the “Workspace” category there is a sub category with the name you chose for your project, this is your project and all of its contents.

Step 36:

Under your project’s title, you will see a folder called “Sources”, this is where the files containing the code you will be writing are found. If it isn’t already showing, click the small triangle to the left of the “Sources” folder to show its contents.

Step 37:

This will open your code at the large text area to the right of the “main.cpp” file you just clicked. You are ready to begin coding!

Step 38: Time to Code

There are two critical pieces of information that you will need to understand what you are doing.

a. First, Variables. Recall from algebra, variables are characters or strings of characters that can represent many different values.

i. For example the variable x could be many different values until you specify it such as, x=10. There are different variable types, x as it was used in the example was an “int” variable, where “int” means integer.

ii. We will be using a “string” variable, which stores a string of characters for your name.

b. Secondly, Functions. While the idea of functions can become quite complex, think of them simply as miniature programs that perform a specific, defined task.

i. We will be using the “cin” and “cout” functions from a library of functions called, “iostream”

Step 39:

In codeblocks, select

File > New > File… > “C/C++ source” > C++ >Select where you want it to save and name it.

Step 40:

Once you have a blank C++ source file, we must include the proper libraries at the top to be able to use our functions and strings

a. Start with “#include “ to allow us to read and write to the command prompt console

b. Next, “ #include ” so that we can use string variable types to read in your first name to a character string variable.

Step 41:

The next step is to tell the computer you will be using the standard namespace

a. Simply typed as, “ using namespace std;”

i. Note: This is the first use of the semi-colon but it most definitely will not be the last. Semi-colons are ubiquitous across many languages as a way to tell the computer you have finished a statement. Very similar to the way that humans use periods.

b. If you are interesting in learning more about namespaces, below is a link to a cplusplus.com forum thread. Very often the best and clearest information is found in these online communities

i. http://www.cplusplus.com/forum/beginner/49748/

Step 42:

Once all the proper libraries have been included and the namespace declared we are ready to start the main section of our program. In C++ and many other languages, the computer will only run what is needed for the main() function. To declare the start of your program the following syntax is used, “int main()” followed by opening brackets, ‘{‘ and closing brackets, ‘}‘ to contain all the functions and variables that will be used.

Step 43:

Inside of the brackets we will declare our first and only variable for the program, “string name;”

a. “string” being the type of variable, a literal string of characters

b. “name” is the arbitrarily defined name of the variable. Feel free to rename it to any continuous string, e.g., firstName, myName, etc.

Step 44:

Next, we will finally begin telling the computer to perform actions. cout<<” is a function statement that will insert any statement in quotations found after it’s insertion operator, “<<”, in to the console when the program is run.

Step 45:

For our example we will use,

a. cout<< "Hello World!!!\n\n <<"What is your name?(Please only enter first name)\n";

b. You will notice the “\n” commands. These simply move the cursor down a line in the same way pressing enter does on Microsoft Word. They are used purely for formatting and readability.

c. Also note that we can continue to insert statements with the insertion operator, <<, until we have ended the cout statement with a semi-colon.

Step 46:

To read in the typed name we will use cout’s complement, “cin”. “cin” uses the extraction operator, >>, which will grab an entire string of characters off the console until it reaches a white space, the area between words. It will then store that string in whatever variable you have after the extraction operator.

Step 47:

Assuming the user has only entered one name, the code remains simple.

a. “cin>> name;”

b. This is telling the computer to read whatever is on the console input and store it in the string variable, name.

Step 48:

We are finally able to display a personalized message. Again, we will use cout.

a. cout<< "Hello, " <

b. The formatting and spacing is critical for readability.

i. The space after “Hello,” in the quotes will add a space before the name is inserted. The exclamation mark before “I hope” finishes off the statement after name has been added.

Step 49:

Last but certainly not least, we must clearly tell the computer that our program has finished running.

a. A simple return statement before the closing brackets will do the trick

b. “return 0;”

Step 50:

To run your completed program

a. Build > Build and run

b. Alternately, press f9 on windows computers

Feel free to change the message and have some fun with it J