CS 101
Lab 6
This lab is to be worked on individually,
but you may feel free to consult with other students.
Objectives
- Learn basic operation of Eclipse
- Using the newly developed knowledge, write, compile, and debug
your first C program
Instructions
Using Eclipse
For this section, please refer to the lecture notes for Lectures 8
,9 and 10.
Creating a Project
- First, run Eclipse from the Applications menu under Programming.
- If asked about a Workspace, just click "Ok". Now, you should see
the Eclipse environment as shown in the figure below.
- Next, create a new C project by following these steps:
- Goto "File" menu, select "New", and then "New Project". You can
refer to the figure below as a guide to locate the "New Project" menu
item.

- Double click on "C" and double click on "Managed Make C
Project".
- Name the project "lab6" (without quotes) and click "Next".
- Click "Finish".
- If asked whether to use the C/C++ Perspective, choose "Yes".
- Locate the "lab6" project on the left side of Eclipse.
- Right click on the "lab6" project and choose "New", then
"Source File"
- Name your file "intro.c"
Entering the code
- Locate the "intro.c" tab in the center of your screen.
- Type in (or copy and paste) the following C program and save it:
If you've programmed in C before, you may notice that there's an
error in the code. This is done on purpose to show you what happens
when you
make a mistake while programming. Use the tab key to automatically
indent properly.
#include <stdio.h>
int main(void)
{
int x = 0;
printf("Enter a number: ");
scanf("%i", x);
if (x % 2)
printf("%i is odd\n",x)
else
printf("%i is even\n",x);
return 0;
}
- Once you finish typing, save your file by
clicking on the Save button in the toolbar.
- When you save, Eclipse compiles your project and shows you any
error messages.
Fixing the errors
- Locate the error underlined in red.
- To see the error message, point your mouse cursor at the
underlined text.
Write down what happens on your answer sheet.
1. Write the warning and error
message or messages that Eclipse
produced:
- Fix the mistake.
- Save again.
Running your program
- Locate the "Run" button on the toolbar (A Green Circle with a
white Play arrow inside). The "Run" button is shown is the figure
below.

- If you see a small drop down menu, Click "Run...". Now, you
should see a dialog as shown in the figure below.
- Double click on "C/C++ Local Application".

- Click the "Search Project" button to see the dialog shown below.

- Click "Ok"
- Click "Run"
- Click in the tab called "Console" at the bottom of the window.
Your program will ask you to enter a number. Type in any number and hit
enter.
Write down what happens on your answer sheet.
2. Write what happens when you run your program for the first time
after it compiles:
Oops, it looks like we have another problem with our program, and
the compiler didn't catch it.
It seemed to happen just after entering the number, so let's look at
the line where our program reads the input.
scanf("%i", x);
(Notice that the scanf line is underlined in yellow. Read the warning
message by pointing your mouse at the underlined code.)
The problem is that this function expects to be given the location
of an integer, but we gave it the integer itself. Since the value of a
was zero, the function tried to use memory at location 0, which caused
a segmentation fault. (and no output in Eclipse.)
To fix the error, we put a & in front of the
variable x.
scanf("%i", &x);
Save and run your program again by clicking the green part of the
"Run" Button.
Have the TA initial your answer sheet and check that your programs
runs. Show your lab TA the code
you entered.
3. TA's initials for Part 1: _______
Congratulations! You have now been initiated into the world of
C and Eclipse.
That's it. You're done for Lab 6!