1 CS 240 Programming in C and UNIX Lecturer: Bob Wilson Office: S-3-071 Phone: 617-287-6475 Email:...

22
1 CS 240 Programming in C and UNIX Lecturer: Bob Wilson Office: S-3-071 Phone: 617-287-6475 Email: [email protected]

Transcript of 1 CS 240 Programming in C and UNIX Lecturer: Bob Wilson Office: S-3-071 Phone: 617-287-6475 Email:...

1

CS 240Programming in C and UNIX

Lecturer: Bob WilsonOffice: S-3-071

Phone: 617-287-6475Email: [email protected]

2

Course Objectives

• This course will teach you C Programming

• It also covers several other related topics:– UNIX Commands– Compiler and Debugger– MAKE and Makefiles– UNIX File System and File Access– UNIX Processes and Shells/Shell Scripts

3

Motivation

• C is the language of choice for systems programming and embedded systems

• Number of Programmers by Language:– COBOL 3 million– Visual Basic 1.5 million– C/C++ 1.1 million

• Mastery of the material in this course may enable you to get a high paying job!

4

Introduction• Syllabus and Lecture Notes

– Web: http://www.cs.umb.edu/~bobw

• Required Textbooks– The C Programming Language, 2nd Ed., Kernighan &

Ritchie– UNIX for Programmers and Users, 2nd Ed., Glass &

Ables

5

Introduction• HW assignments

– Assignment write-ups will be on my website– But HW must be done on our UNIX systems!

• All Homework MUST BE an Individual Effort– You can answer questions for each other as long as you

acknowledge any help that you receive from others– BUT DON’T HAVE OR LET ANYONE ELSE DO YOUR

HOMEWORK FOR YOU!– Don’t change the owner, group, or mode on any of your

homework directories or files!! (NO chown, chgrp, chmod!)– If I discover any cheating, I’ll follow school policy!

6

Getting Started• Fill out the CS240 Student Survey Form on the

web page as soon as possible• UNIX Account

– Apply for CS240 ASAP! – (Science 3rd Floor UNIX Lab)

• Use Sun Blades in UNIX Lab• Access to UNIX systems from your home PC

– Secure internet access is required now!– Can not use TELNET or FTP for remote access!– Must use Secure Shell 2 for remote access– Can use Putty or SSH Communications S/W packages

7

Getting Started (SSH 2)• Putty (Officially Recommended by Systems Staff)

– http://www.puttyssh.org (Non Commercial Downloads)– Download: SSHSecureShellClient-3.2.5.exe

• SSH Communications (Recommended by me)– http://www.ssh.com/support/downloads/secureshellwk– Download Non-Commercial Version (.exe for Windows)– Or http://ce.uml.edu/sshwinclient.exe– Download and Execute Installer executable

• Student Recommended– http://www.i-tree.org/gpl/ixplorer.htm (Windows)– http://rsug.itd.umich.edu/software/fugu/ (MAC OS X)

8

Getting Started (Editor)• Editing source files is a requirement

• Learn to use a UNIX editor program– vi: a simple visual editor described in Glass– emacs: a more complex visual editor that can

also be used as a complete development shell (also described in Glass)

– pico: a simple text editor based on the pine email program ( I personally like this one!)

9

Getting Started (Email)• Use the UNIX program pine for your email

• Pine is supported by system staff – UNIX mail program is not.

• Type command “man pine” for pine user documentation

• Type command “pine” to access email

10

Communication• You must monitor course web site and your UNIX

email account for announcements!!• To contact me:

– Office: S-3-071

– Office Hours: • Posted on my website

• UNIX: finger bobw

– Email: [email protected]

– Phone/Voice Mail: (617) 287-6475 (UMB)

– or (508) 668-7427 (Home) only if really stuck!

11

Java versus C• CS110 (Java) is a prerequisite for CS240!• Java and C are both “block structured” languages

– Compound statements defined by braces { }– Similar if-else, for/while loops, expressions, etc

• Java is Object-Oriented, C is only procedural• Java and C syntaxes look deceptively similar• However, many small annoying differences

– Close but different semantics/syntax can be confusing– Learn C as a different language – not just like Java!– C99 is a bit closer to Java, but we’re not using it here!

12

Java versus C• Java Compiler / Interpreter

– Java source is compiled to byte code file (.class)– Byte code file is then interpreted by JVM– JVM is written in native machine code– Interprets program slower than native machine code

• C Compiler / Linker– C source is compiled to object file(s)– Object file(s) is/are linked to create an executable file– Executable file runs as native machine code– Executes program faster than interpreting byte code file

13

C programming

• You will be learning to write, execute, and debug C language programs in this course

• We will not be just modifying programs that have already been written and debugged!!

• We will spend most of the time in class on the C language, MAKE, and the debugger

• This is the primary material for tests/quizzes• Use Kernighan and Ritchie (K&R) textbook!!

14

UNIX Operating System

• You will be using UNIX to edit, compile, debug, and run your C programs.

• We will not spend as much time in class on UNIX.• You must learn to use UNIX as you go• You can be held accountable for UNIX on exams• Use UNIX Guide and Glass textbook• Also can refer to the Umass Boston Website:

– http://www.cs.umb.edu/helproot/cs.guide/csguide/csguide.html

15

Basic UNIX Commands(Responsible for these next time)

cat display a file on your terminal screen (see also “more”)cd change directorycp copy a filelogout logout from your accountlpr print a hard copyls list files in a directoryman xxx manual page for command xxxmore display a file on your terminal screen - one page at a timemv move a file from one place to anothermkdir create a new subdirectorypwd print working directory (pathname of directory you’re

in)rm remove (delete) a filermdir remove (delete) a directoryCTRL-c “Control” key and “c” key together – stop current command

16

UNIX File SystemDirectory

Sub-directory

Sub-directory File

File

bobw

~bobw/cs240

~bobw/cs240/hw0

~bobw/cs240/hw0/assignment

ls

cd cs240 cd ..

rmfilenameNew Sub-directorymkdir

pwd

17

Hello World!• Your first homework project is to create and run a C

program – “Hello World!” (K&R, p5+)• Create a source file “hello.c” in one of three ways

– Use Unix Systems in UNIX Lab with vi, pico, or emacs– Use Putty/SSH on PC to access UNIX with vi, pico, or emacs– Use Notepad on your own PC and transfer the file

• Use “gcc” to compile and create a file named “hello”• Run “hello” to see the printout on screen• Use a “typescript” file to turn in assignments

18

hello.c (K&R, Page 6)/* hello.c: first homework assignment

name: your namedate: xx/xx/xx

*/#include <stdio.h>int main( ) /* we’ll ignore arguments to main for now */{

printf(“Hello World!\n”);return 0;

}

19

C Source - Comment Lines• Comment text is ignored by the compiler

/* This is a multi-line comment.The compiler ignores both lines. */

• Be sure to include the closing “*/”/* This is a multi-line commentint main ( ){

printf (“Hello World!”);return 0;

} /* terminated by this -> */

20

C Source - #include …

• Because this program uses the Standard I/O Library, it needs to include <stdio.h>

• In C programming, a “.h file” defines – Macros (e.g. Names for constants)– Prototypes for functions (e.g. printf itself)

• “gcc won’t compile “hello.c” with “printf” function without the “#include <stdio.h>”

21

C Source – Main Declaration

• “int main ( )” is the function where the UNIX system starts execution of your program

• Your function’s code is within “braces” { }

• Braces are usually placed on their own lines

{

function statements are here;

}

22

C Source - printf

• The Standard I/O Library provides you with a function named “printf ( )”

• “printf ( )” prints argument as text on screen• Argument is the text between the parentheses

(“Hello World!\n”)• “\n” is a C convention for “end of line”

(For open book tests, remember K&R page 193)

• All C program statements end with a “;”