introductioncs-2301, b-term 20091 cs-2301, system programming for non-majors (slides include...

41
Introduction CS-2301, B-Term 20 09 1 CS-2301, System Programming for Non-Majors CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 1

CS-2301, System Programming for Non-Majors

CS-2301, System Programmingfor Non-Majors

(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

Page 2: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 2

CS-2301 Course Objectives

• C language programming• Designing, implementing, debugging, etc.

• Basic understanding of how computers execute C programs

• … and other kinds of programs

• Preparation for computational challenges of engineering, scientific, and other professions

• Preparation for upper-level courses in computing• “Thinking computationally”

Page 3: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 3

Why?

• Computing and Programming: a life skill for all technical professionals

• Thinking Computationally: organizing your engineering/scientific/technical thoughts to be amenable to computational solutions

Page 4: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 4

Why C?

• Because we have to!

• Many situations outside of CS where it is only language or system available

• Small, embedded systems, instrumentation, etc.

• Many “low-level” situations that don’t have support for “high-level” languages

• Operating systems, real-time systems, drivers

Page 5: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 5

Why not C?

• C is very low-level• Data structures must be programmed “by hand”• Operations must be done out in “long hand”• No support for “object oriented” design• Marginal support for higher-level thought processes• Much, much harder to use than higher level languages/systems

• Better alternatives available for technical applications• Verilog, VHDL, System C – semiconductor design• Matlab, SimuLink – physical modeling• LabView – instrumentation and control• Excel – accounting and statistics• SQL – billing and transactions• …

Page 6: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 6

This Course

• Lectures• C programming language, syntax, semantics, etc.• Common data structures that technical professionals

are likely to need or encounter in C

• Programming Assignments• Write programs to exercise various language

constructs and data structures• Mandatory for passing this course

• Lab Sessions• How to use the system, tools, debuggers, etc.• Practical help from TAs, etc.

Page 7: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 7

This Course (continued)

• Tuesdays & Fridays, 2:00–3:50 PM• Higgins Labs 116

• Weekly Lab Sessions• B01 – Higgins Labs 230, Wednesdays 2:00-2:50 PM• B02 – Higgins Labs 230, Wednesdays 3:00-3:50 PM

• Three exams (one hour each)• November 13• December 1• December 15

• Review session prior to exam on same day

Note: Exams are deliberately designed to be difficult.

Median grade about 50%; graded on a curve — e.g., median ± 10% B.

Include writing program fragments.

Closed book, one 8½-by-11 sheet of prepared notes, no calculators or electronics

Page 8: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 8

Prerequisites

• CS-1101 or CS-1102• Or other “first” programming course

• Or programming job, or other experience

• Background survey

• Note: credit may not be earned for both CS-2301 and CS-2303

Note:– • If you have never programmed a computer

before,• And if you are taking an overload this

term,• Chances are that you will not do well in

this course.

Page 9: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 9

Logistics

• Course web site– http://www.cs.wpi.edu/~cs2301/b09

• Professor’s office Hours– Tuesdays and Fridays, 1:00 PM – 1:50 PM, or by appointment

(additional hours TBD)– Fuller 144:– (508) 831-5493

• Contacts– <Professor’s last name> @ cs.wpi.edu

• Class e-mail lists– cs2301-all or cs2301-staff @ same domain

• Teaching assistants– Feng Li (TA) – Medhabi Ray (TA)– Andrew Keating (SA)

Page 10: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 10

Required Textbook

• The C Programming Language, 2nd edition, by Brian Kernighan and Dennis Ritchie, Prentice Hall, 1988

Bring to all classes and all lab sessions

Page 11: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 11

Textbook Outline

• Chapter 1: a tutorial aimed at people who already know how to program

• Chapters 2-8: description of C language• Slightly different order from Chapter 1

• Appendix A: the C language reference• Official definition of language is here!

• Appendix B: the standard libraries• You will use these a lot!

Page 12: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 12

Ground Rule #1

• There are no “stupid” questions.

• It is a waste of your time and the class’s time to proceed when you don’t understand the basic terms.

• If you don’t understand it, someone else probably doesn’t it, either.

Page 13: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 13

Ground Rule #2

• Help each other!

• Even when a project or assignment is specified as individual, ask your friends or classmates about stuff you don’t understand.

• It is a waste of your time try to figure out some obscure detail on your own when there are lots of resources around.

• When you have the answer, write it in your own words (or own coding style).

Page 14: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 14

Names and Faces

• It is in your own interest that I know who you are.

• Students who speak up in class usually get more favorable grades than those who don’t

• When speaking in class, please identify yourselves

Page 15: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 15

WPI Academic Honesty Policy

• It is a violation of the WPI Academic Honesty Policy to submit someone else’s work as your own.

• It is not a violation of WPI’s Academic Honesty Policy to ask for help!

• Classmates, TAs, friends, mentors, …

• Explanations of things you don’t understand

Page 16: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 16

Additional Help

• Academic Resource Center has Tutors available to assist in CS 2301.

• The schedule is posted and copies are available on the door of the M*A*S*H room and Tutor Center, Daniels Hall, 1st floor.

Page 17: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 17

Instructor — Hugh C. LauerAdjunct Professor

• Ph. D. Carnegie-Mellon, 1972-73– Dissertation “Correctness in Operating Systems”

• Lecturer: University of Newcastle upon Tyne, UK• Approximately 30 years in industry in USA• Research topics

– Operating Systems– Proofs of Correctness– Computer Architecture– Networks and Distributed Computing– Real-time networking– 3D Volume Rendering– Surgical Simulation and Navigation– …

Page 18: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 18

Systems and Programming Experience

• IBM Corporation• University of Newcastle• Systems Development Corporation• Xerox Corporation (Palo Alto)• Software Arts, Inc.• Apollo Computer• Eastman Kodak Company• Mitsubishi Electric Research Labs (MERL)• Real-Time Visualization

• Founded and spun out from MERL• Acquired by TeraRecon, Inc.

• SensAble Technologies, Inc.• Dimensions Imaging, Inc. (new start-up)

Page 19: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 19

Experience (continued)

• CTO, Chief Architect of VolumePro™ 1000• World’s first interactive 3D rendering engine for CT, MRI,

seismic scans for PC-class computers• 7.5-million gate, high-performance ASIC• 109 illuminated graphical samples per second

• Two seminal contributions to computer science• Duality hypothesis for operating systems (w/Roger Needham)• First realization of opaque types in type-safe languages (with

Edwin Satterthwaite)

• 21 US patents issued• Computer architecture• Software reliability• Networks• Computer graphics & volume rendering

Page 20: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 20

Questions?

Page 21: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 21

The C Language

• First created to develop Unix – late 1960s• Kernighan & Ritchie, 1st edition – 1978

• ANSI C – 1988• Kernighan & Ritchie, 2nd edition, 1988• Implemented by nearly all C compilers

• C95, C99• Minor additions (to be noted as we get to them)• Most major C compilers

Page 22: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 22

Successors to C

• C++• Developed by Bjarne Stroustrup and team at Bell

Labs• Major extension of C to support object-oriented

programming• Attempted to preserve syntax and structure of C

• Java• Rewrite of C++ at Sun Microsystems• Machine independence, portability• Ability to embed in web pages• Huge libraries of packages for all kinds of stuff

Page 23: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 23

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

Page 24: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 24

Fundamental Rule in C

• Every identifier must be declared before it can be used in a program

• Definition:– “identifier”• A sequence of letters, digits, and ‘_’• Must begin with a letter or ‘_’• Case is significant

– Upper and lower case letters are different

• Must not be a “reserved word” — see p. 192

• Definition:– “declare”• Introduce an identifier and the kind of entity it refers to• Optionally, define associated memory or program

Page 25: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 25

So where is printf declared?

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

Page 26: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 26

So where is printf declared?

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

Answer: in this file!

Page 27: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 27

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• A header file• Contains declarations

of names, functions, data, of things defined elsewhere

• E.g., by the system

• Text of the header file is inserted by compiler into your program

• As if you wrote it yourself!

Page 28: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 28

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• A function declaration• Declares the name and

defines the body of your function

• May take arguments, returns an integer

• main is a special name to the system

• The place where a program “starts”

Page 29: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 29

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• Body of the function• Defines what the

function “does”• Sequence of

statements• Each does a step of the

function

• Enclosed in curly brackets

•{ }

Page 30: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 30

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• Call to another function • In this case, a function defined by the system

• Prints some data on standard output

Page 31: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 31

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• Argument to printf – a constant string• Enclosed in straight double quotes

• Note the new-line character ′\n′ at the end

Page 32: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 32

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• A return statement• return is a reserved word in C

• main should return zero if no error; non-zero if error

Page 33: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 33

Your First C Program

#include <stdio.h>

int main (void) {

printf(″Hello, World!\n″);

return 0;

}

• Note that statements typically end with semicolons• So compiler can tell where end of statement is

Page 34: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 34

Questions?

Write, compile, and execute this program in Lab session tomorrow

Page 35: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 35

What happens to your program …

…after it is compiled, but before it can be run?

Page 36: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 36

Example

#include <stdio.h>

int main (void) {

printf (″Hello,″ ″ world\n″);

return 0;

}

• Symbol defined in your program and used elsewhere

•main

• Symbol defined elsewhere and used by your program

•printf

Page 37: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 37

Static Linking and LoadingPrintf.c

Printf.o

Librarygcc

ar

Linker

Memory

HelloWorld.c

gcc

HelloWorld.o

Loader

a.out(or name of

your command)

Page 38: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 38

Compiling Your Program• gcc HelloWorld.c

• Compiles the program in HelloWorld.c, links with any standard libraries, puts executable in a.out

• You should find HelloWorld.o in your directory

• gcc –o hello_world HelloWorld.c• Same as above, but names the executable file hello_world instead of a.out

• gcc –lrt HelloWorld.c• Searches library named rt.a for functions to link

(in addition to standard libraries)

Page 39: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 39

Compiling Your Program (continued)

• gcc foo.c bar.c help.c• Compiles the programs foo.c, bar.c, and help.c,

links with standard libraries, executable in a.out• You should find foo.o, bar.o, and help.o in your

directory

• gcc –o Lab2 foo.c bar.c help.c• Same as above, but names the executable file Lab2

• gcc –c foo.c bar.c help.c• Compiles foo.c, bar.c, and help.c to foo.o, bar.o, and help.o but does not link together

Page 40: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 40

Questions?

Page 41: IntroductionCS-2301, B-Term 20091 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by

IntroductionCS-2301, B-Term 2009 41

Short Break

Please fill out survey questionnaire