how to create your first computer program in c

8
Your First C Program 1 How to Create Your First Computer Program in C By: Shawn R Moser Programming can be a difficult field to get into. For those looking to breakthrough, the first step on the path to learning programming starts with making what is called a Hello, World program, a program that simply displays the text Hello Worldto the computer screen. This guide will show you how to create this program, using the programming language C, a widely used programing language. The guide will help you get through the process of writing this first program through four distinct steps: 1.) First, you will download an application on your computer called Code::Blocks, which you will use to create the code for the program. 2.) Next, you will install the Code::Blocks software on your machine. 3.) Then, you will create a project in Code::Blocks to hold your first program. 4.) Finally, you will build and run the project that holds the code for the Hello Worldprogram. Checklist before Beginning In order to create this program, it is assumed that you currently have access to a working computer system in which Code::Blocks can be installed to create your first C program. Please ensure the following before beginning this guide: You have prior experience navigating a computer and installing downloading software. It is assumed that you are aware of how to: install programs, navigate files, and use an internet browser to download files. If you are unsure of how to perform any of these tasks; consider looking at sources at your local bookstore on basic computer functionality. You meet all the technical requirements necessary to install the Code::Blocks software. The system should have Windows 7/8 for the operating system. You will be able to use any Windows operating system from Windows XP on, along with any Linux or Mac OS distribution, but the guide assumes the use of Windows 7 or 8. The system should have at least 250 MBs of free hard-drive space. This space is required to hold install Code::Blocks on the system. You have permission to install software on your computer system. Ensure that you have administrative privileges for the system you will be writing the program on. If not, check with your local system administrator about installing software on the computer.

Upload: others

Post on 19-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Your First C Program 1

How to Create Your First Computer Program in C

By: Shawn R Moser

Programming can be a difficult field to get into. For those looking to breakthrough, the first step on the

path to learning programming starts with making what is called a Hello, World program, a program

that simply displays the text “Hello World” to the computer screen. This guide will show you how to

create this program, using the programming language C, a widely used programing language. The

guide will help you get through the process of writing this first program through four distinct steps:

1.) First, you will download an application on your computer called Code::Blocks, which you will

use to create the code for the program.

2.) Next, you will install the Code::Blocks software on your machine.

3.) Then, you will create a project in Code::Blocks to hold your first program.

4.) Finally, you will build and run the project that holds the code for the “Hello World” program.

Checklist before Beginning

In order to create this program, it is assumed that you currently have access to a working computer

system in which Code::Blocks can be installed to create your first C program. Please ensure the

following before beginning this guide:

□ You have prior experience navigating a computer and installing downloading software.

► It is assumed that you are aware of how to: install programs, navigate files, and use an internet

browser to download files. If you are unsure of how to perform any of these tasks; consider

looking at sources at your local bookstore on basic computer functionality.

□ You meet all the technical requirements necessary to install the Code::Blocks software.

► The system should have Windows 7/8 for the operating system.

You will be able to use any Windows operating system from Windows XP on, along with any

Linux or Mac OS distribution, but the guide assumes the use of Windows 7 or 8.

► The system should have at least 250 MBs of free hard-drive space.

This space is required to hold install Code::Blocks on the system.

□ You have permission to install software on your computer system.

► Ensure that you have administrative privileges for the system you will be writing the program on.

If not, check with your local system administrator about installing software on the computer.

Your First C Program 2

Downloading Code::Blocks

First, in order to setup the program to create your code, you will use and setup an integrated

development environment called Code::Blocks. An integrated development environment, or IDE for

short, is a program used to edit and create software all in one single package. It is akin to a word

editor or desktop publisher that would be used to create documents. To get Code::Blocks on your

system, you will need to download the binaries from the Internet.

1. Go to the Code Blocks website at

http://www.codeblocks.org

2. Find the link on the left-hand side

bar labeled Downloads.

3. From this page you will select

Download the binary releases.

There are other options that can

be used to download the source

code, the actual code used to

create the IDE software. There

are reasons you would want to

do this, but it is noted that this

option would require experience

in building such programs; a skill

assumed that you currently does

not have.

Figure 1: (Step 2) Code::Blocks’ Homepage

Figure 2: (Step 3) Code::Blocks’ Download Page

Your First C Program 3

4. Select the codeblock-13.12 mingw-setup.exe option. To download click on the link labeled either

BerliOS or Sourceforge.net on the right side of the screen. The file should now download

automatically to your computer.

Figure 3: (Step 4) Code::Block's Binary Page

Troubleshooting: If the application does not download from either of these links, please

check that you do not have a pop-blocked turned on, nor do you have any other program

stopping downloads from the internet. This can vary depending on system setup.

Installing Code::Blocks

After finishing downloading the Code::Blocks software, you must now install it on the local computer

system.

1. Navigate to the location that you have

downloaded the executable to. This should

be the normal location for downloads on a

Windows 7 or Windows 8 environment.

2. Right-click the file and select Run as

Administrator. This will bring up the

installation screen for the software.

Troubleshooting: If you see a warning, note that this action will make a change to the

system. Simply click “Yes”. This allows the software to be installed on your system.

3. Click Next > to advance.

4. Read the license agreement and click the I

Agree button if you agree to it. If you do not

agree, you will be unable to install the

software.

Figure 4: (Step 2) Run Program As Administrator

Figure 5: (Step 4) License Agreement

Your First C Program 4

5. Select Standard: Core plugins,

core tools, and core lexers

option from the drop down box on

the screen and click Next >.

6. Keep the preset destination folder

for the installation.

7. Select Install and Code::Blocks

will now install.

Troubleshooting: This installation will require at least 250 MBs of space on the system the

software will be installed on. Ensure that there is sufficient space for this on the computer

system.

8. Once the IDE installs you will see a screen listing complier auto-detection. Double check that the GNU

GCC Compiler is detected. If it is selected, press OK to continue.

Figure 6: (Step 5) Component Installation Selection

Figure 7: (Step 6 & 7) Installation Destination Selection

Your First C Program 5

9. Next you will see the options for file associations of C/C++ source files. If you plan to continue

programming and using the particular IDE, then select the Yes, associate Code::Blocks with C/C++

file types. If not, then simply select No, leave everything as it is.

Troubleshooting: If you are not the administrator for the computer you are performing the

installation on, please check with the appropriate authority before changing the file-type

association of the system.

10. When you see the text says Completed

on the screen, click Next >.

11. Click Finish to complete the installation

and start the Code::Blocks program.

Creating a Project in Code::Blocks

The Code::Block IDE is now installed on your computer. The next step to creating your first “Hello

World” program will be to create a new project that will be used to hold the code for your program.

With the IDE now opened from the previous step, perform the following:

1. Select Create a new project from the

center screen.

Figure 8: (Step 10) Installation Completion Screen

Your First C Program 6

2. Click the option Console application

from the template set.

3. Select Go on the right-hand side of the

window.

4. Select the option for C to select that as the

language to use.

5. Select Next > to continue.

6. Give the project a name to be referred to later

on. For this first program simply type in

helloworld.

7. Pick a destination directory to create the

project in. It does not matter where on your

computer you put this folder, in this example

the directory C:\Users\Shawn\Desktop\ was

selected as the root directory.

8. Select Next >.

Figure 9: (Step 1) Code::Blocks Opening Screen

Figure 10: (Step 2 & 3) Application Type Selection Screen

Figure 11: (Step 4 & 5) Language Type Selection Screen

Figure 12: (Step 6, 7, & 8) Project Title Selection Screen

Your First C Program 7

9. In the drop-down box labeled Complier,

select the option GNU GCC Compiler. Keep

the rest of the settings to their default.

10. Select Finish to complete the setup of the

project.

Building the “Hello World” Program

You have now successfully setup a project in which to place the code for your first C program to be

run in. Next, you will be checking the template code that the Code::Block IDE has created and build

the program to run.

1. In the upper-left hand corner of the screen you should

now see a little folder labeled Source. This is a

directory that holds all of the files. Click on the

directory to see the files within the directory. You

should now see a file labeled main.c.

2. Double click the main.c to open it in the IDE. You

should now see the code to your first program listed in

the center screen of the IDE.

3. Look over the code that appears in the

center screen. It should match what is

shown in figure 15.

Figure 13: (Step 9 & 10) Complier Selection Screen

Figure 14: (Step 1 & 2) Project Tree

Figure 15: (Step 3) “Hello World” Code

Your First C Program 8

4. Click on the button that resembles a small

gear above where the code is. This will build

the project, and display the message Process

terminated with status 0 (0 minute(s), 0

second(s)) on the screen.

5. Click on the button that resembles a small

green arrow, to the right of the gear-shaped

button. This will run the program. You should

now see a black screen displaying the

message “Hello world!” on the screen, as

shown on figure 17.

Troubleshooting: If you are having trouble building the code. Please check that that PATH

system variable on your system was correctly setup during installation. Information on this can

be found by searching PATH environment variable online. It should point to C:\Program Files

(x86)\CodeBlocks\MinGW\libexec\gcc\mingw32\4.7.1 if you used the default installation

settings.

Figure 17: “Hello World” Program Successfully Run

Congratulations! You have successfully created your first C program. Now with the IDE setup and

your first program created, you are now ready to venture further into the world of programming. There

are many sources for C-related programming guides, and with a simple search can find many that

can start you on a path to building more powerful and creative programs.

Figure 16: (Step 4) Build the “Hello World” Code