6
5 | Page Lab1–BeginningCProgram Overview This lab covers the basics of compiling a basic C application program from a command line. Basic functions including printf() and scanf() are used. Simple command line concepts are also practiced. Finally practice with the online submission system is emphasized. Prelab Please be aware that for most labs the student needs to have completed the Prelab prior to the lab time. Many prelabs require the student to turn in some evidence of completing the Prelab. Additionally, most weeks there will be a short in lab reading quizzed based on the Prelab questions. Students should be sure they are prepared prior to lab. For this lab, a Prelab is not required. Prelab Quizzes Normally, every week there will be a ‘Prelab quiz’ given the first 5 minutes of lab. This quiz is closed book and must be completed by 5 minutes after the hour. Don’t be late to lab. Procedure Software tools For the first few labs, the course will be covering some of the basic concepts of C using command-line techniques. This type of programming is ‘bare bones’ meaning that it is as close to the process of compiling code possible where individual steps in the process can be seen. Another type of programming environment (program) is an integrated development environment (IDE). These are very common in modern programming but often cost SIGNIFICANT amounts of money to purchase and use. Figure 1: Simple model for developing software. The basic process of compiling code, figure 1, is outlining what needs to be done, writing the code, compiling and linking, and testing. Starting out, you will be using a text editor program (not a word processor) to write your code. On windows, Notepad++ is recommended and is what will be used in these examples. On Mac, choose your favorite tool and Gedit is recommended for Linux. A link for the Notepad++ is available on the lab webpage. To compile the code, we will be compiling on a remote system. This remote system is called flip.engr.oregonstate.edu or lovingly called ‘flip’ for short. To access flip, you need an SSH Terminal program. Putty is

Lab 1 – Beginning C Program - Oregon State Universityeecs.oregonstate.edu/education/docs/cs151/Section1.pdf · This type of programming is ‘bare ... the answers to the study questions

  • Upload
    hadang

  • View
    218

  • Download
    5

Embed Size (px)

Citation preview

5 | P a g e

Lab1–BeginningCProgram

Overview

This lab covers the basics of compiling a basic C application program from a command line. Basic functions including

printf() and scanf() are used. Simple command line concepts are also practiced. Finally practice with the online

submission system is emphasized.

Prelab

Please be aware that for most labs the student needs to have completed the Prelab prior to the lab time. Many prelabs

require the student to turn in some evidence of completing the Prelab. Additionally, most weeks there will be a short in

lab reading quizzed based on the Prelab questions. Students should be sure they are prepared prior to lab. For this lab, a

Prelab is not required.

Prelab Quizzes

Normally, every week there will be a ‘Prelab quiz’ given the first 5 minutes of lab. This quiz is closed book and must be

completed by 5 minutes after the hour. Don’t be late to lab.

Procedure

Softwaretools

For the first few labs, the course will be covering some of the basic concepts of C using command-line techniques.

This type of programming is ‘bare bones’ meaning that it is as close to the process of compiling code possible where

individual steps in the process can be seen. Another type of programming environment (program) is an integrated

development environment (IDE). These are very common in modern programming but often cost SIGNIFICANT

amounts of money to purchase and use.

Figure 1: Simple model for developing software.

The basic process of compiling code, figure 1, is outlining what needs to be done, writing the code, compiling and

linking, and testing. Starting out, you will be using a text editor program (not a word processor) to write your code.

On windows, Notepad++ is recommended and is what will be used in these examples. On Mac, choose your favorite

tool and Gedit is recommended for Linux. A link for the Notepad++ is available on the lab webpage.

To compile the code, we will be compiling on a remote system. This remote system is called

flip.engr.oregonstate.edu or lovingly called ‘flip’ for short. To access flip, you need an SSH Terminal program. Putty is

6 | P a g e

recommended for windows and the default terminal can be used on mac or Linux. A link for the putty is available on

the lab webpage.

Finally to make it easier to work on files in windows, a tool for mapping network drives should be installed. On the

ENGR computers in the labs this is done automatically. The ‘Z’ drive is a mapped drive to the location on flip used in

this lab.

Mapping a Network Drive using a Windows Laptop

If you would like to use your own laptop for this course (recommended) setting up a windows network drive is

simple. NOTE: The mapped drive will only work while the laptop is connected to the OSU Wireless network.

Open “Computer” and select “Map Network Drive”:

From the Start menu navigate to Computer, found on the left

quick navigation bar. This can also be found by opening the Start

menu and selecting “Computer” From the choices on the right.

Once here click on “Map Network Drive”, which should be the

forth entry on the under the navigation bar, Figure 2.

Figure 2:

Enter in the network address of your engineering home folder.

Select a drive letter from the drop down menu for your

engineering home folder to be mounted, the default is the Z:

drive. Then enter the network address of your home folder

(without brackets):

\\stak.engr.oregonstate.edu\[Your_Engineering _Username]

Finally select “Connect using different credentials” and

“Reconnect at Logon” press “Finish”, Figure 3

Figure 3

Step 3: Enter in your Engineering Credentials

Username: ENGINEERING\[Your Engineering Username]

Password: Your Engineering Password

After selecting OK, there will be a new drive under Computer

from the Start menu.

Figure 4

7 | P a g e

Createdirectoriesanddownloadfiles

To get started, the directory for the code needs to be created on the flip server. To do this you must have an ENGR

account. If you have not set one of the up prior to this course, please consult your TA to get direction how to do it.

To login into flip, open up Putty, (or another

SSH application), figure 5, and fill in the

address information. Remember, you may

need to download putty from the lab webpage

to use it!

Host Name: flip.engr.oregonstate.edu

Figure 5:

The first time a connection is made the

software will ask if it is ok to use and store a

‘public key’, figure 6.

Allow the connection and enter the user name

and password when requested (no figure).

Figure 6:

Select the default terminal (usually [XTERM])

by pressing [ENTER] when asked, figure 7.

A ‘prompt’ will be displayed, figure 8, type

bash and press enter. Note: Things that should

actually be typed into the keyboard will appear

in bold.

Figure 7:

Figure 9:

Figure 8:

cd – Change Directory

cd .. – Change Directory ‘Up’ a Level

ls – List Directory Contents

mkdir – Make Directory

pwd – Present Working Directory

8 | P a g e

Working at the command line is different than using a graphical user interface because it allows for much faster

control and execution of programs. Just like with a graphical interface, the hardest thing is learning where the

different programs are located. The find programs and files, rather than using the mouse and selecting icons,

commands are typed into the command window and ‘directories’ are navigated. Figure 9 has a list of common

commands, but for now type mkdir ece151 and press enter. Note: After any command pressing enter is required to

make the command operate. It will not be mentioned in the rest of the document. Run the following sequence of

commands, figure 10:

cd ece151

mkdir lab1

cd lab1

pwd

Figure 10: Example of output after running

commands

The present working directory (pwd) command shows what directory the user is currently in. In the example, the

directory will be different, but the last part ‘/ece151/lab1’ must be the same for the following commands to work.

Download the lab1 files from the lab webpage. Unzip them into the directory created above. This can be done by

using the windows file explorer and navigating to your ‘Z:’ drive. Once there, you can navigate to the \ece151\lab1

directory.

After the files are unzipped, type ls into the putty window to get a directory listing.

EnteringCcode

In the directory there is a c file called ‘main.c’ Open this file with Notepad++ for entering code. Once it is open, enter the

code snippet below and save the file.

#include <stdio.h> int main (void) { printf("Hello CS/ECE 151!\n"); return 0; }

There are several special characters in this code snippet that are important to the C language. Notice the semicolons at

the end of each command. These must be there to tell the compiler that the command is complete. Also notice the curly

braces ( ‘{‘ and ‘}’ ) and how there is the same number of ‘open braces’ (‘{‘) and closed braces (‘}’). These braces enclose

blocks of code that are run together. For example in this code snippet, the printf() and return() statements are both part

of the main() function.

9 | P a g e

CompileCfile

After entering the code snippet for lab 1, the next step would be to compile. On most systems there is a process to

compiling and linking. There are often many steps to be taken. Often these steps are put into a ‘makefile’ so that a single

command can be used to compile and link. The command for this is make all try it at the command line. The code should

compile without errors. The resulting compiled program is ready to run. To run it enter labprogram into the command

window.

IntroduceanError

Notice that each line has a ‘line number’ that helps to locate specific lines easily. When errors are found in the code

by the compiler the line number of the error is displayed so it can be founds easily in the code.

To understand this, introduce an error by deleting the semicolon from the end of line with the printf() statement.

Save the file and go back to the putty program. Ensure that the present working directory has /ece151/lab1 at the

end.

Run the make all command. Examining the output, an error should be visible, figure 11.

Figure 11: Example of a compilation error.

Compile C File

Working from the initial program supplied, edit the program to print out the following text. Make sure the program

prints out information that is specific to you, not the exact information below:

User: heer

Name: Donald Heer

Favorite Color: Blue

DemonstrateandSubmitCFile

For every lab assignment, two things must be done. First, the directory where the code is found (eg. lab1) must be

zipped into a file and submitted via the TEACH interface prior to the end of lab time. Additionally by the end of lab, the

working code must be demonstrated to the lab TA for the course. To get credit for the lab, both tasks must be

accomplished. The assignment name for this submission is:

TEACH Assignment Name: Lab1

Before having the teaching assistant come to see the demonstration of the code, ensure that the program has already

been started and that source code is already open for the TA to inspect.

10 | P a g e

Study Questions

Many of the labs will have assigned study questions. When assigned, the answers to the study questions should be

printed out and handed into the TA the lab following. If no study questions are assigned, nothing would be turned in

along with the demonstration.

1. In your own words, what are each of the following Linux commands; pwd, cd, rm, cp, mkdir, rmdir, and ls.

2. Where is the computer science freshman mentor lounge? What is the poster of on the wall?

Extended Learning

Extended learning is an opportunity for you to try out some new things and take your knowledge to the next level. You

are not required to do anything under the extended learning section. However, if you do successfully finish all of the

extended learning sections for the course by their due dates, you can choose to opt out of the final for the course as

long as you have a C average or better on the other tests in the course.

Whattodo

The extended learning challenge for this first lab is to explore how to use the function scanf() to take in three variables;

username, first name, and favorite color. Once the user has entered all three items, the program should print them out

in the same format as in the above lab.

DemonstrateandSubmitCFilewithComments

For every extended learning assignment, two things must be done. First, the directory where the code is found (e.g.

lab1) must be zipped into a file and submitted via the TEACH interface prior to the beginning of lab time the following

week. Additionally at the beginning of the next week’s lab, the working code must be demonstrated to the lab TA for the

course. To get credit for the lab, both tasks must be accomplished. The assignment name for this submission is:

TEACH Assignment Name: Lab1Extra

Lab Summary

Task Completed?

Install Software (Optional)

Download and compile sample code

Introduce an error and see output

Modify code to print new information and demonstrate

Extended Learning demonstration (Optional)