cs240: programming in c - purdue university · 2011-01-09 · history the world in 1969 fortran,...

24
Suresh Jagannathan Lecture 1/ Spring 2011 1 CS240: Programming in C Lecture 1: Class overview. 1 Saturday, January 8, 2011

Upload: others

Post on 10-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Suresh Jagannathan Lecture 1/ Spring 2011 1

CS240: Programming in C

Lecture 1: Class overview.

1

Saturday, January 8, 2011

Page 2: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

History The world in 1969

Fortran, PL/1 and Cobol were the primary “high-level” programming languages available.

Assembly language the choice for high-performance applications

Operating systems • A golden age• Advent of paging, segmentation, virtual memory,

and access-control file systems

2

Saturday, January 8, 2011

Page 3: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Multics An advanced operating system

developed at MIT, GE and Bell Labs. Precursor to many of the ideas found in

Unix process as a locus of control tree-structured file system advanced access control generalized device abstraction command interpreter

Written almost entirely in PL/1 and BCPL

3

Saturday, January 8, 2011

Page 4: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Target hardware environment Port to DEC PDP-7

8K 18-bit memory no useful software port written in assembler

First high-level language on Unix TMG: a language for writing compilers

• primitive support for context-free syntax First general-purpose high-level language

B: C without types interpreted ran in 8K bytes of memory

4

Saturday, January 8, 2011

Page 5: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Circa 1970 - 1971 Unix had developed a sizable user base B was too slow; assembler too painful

string manipulation painful on byte-oriented machines

no floating-point pointers were not easily manipulable

• a pointer was an index into an array of words Extend B with (richer) types

Further additions of record types and typed arrays

Unified type syntax

5

Saturday, January 8, 2011

Page 6: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

By 1973 Preprocessor Integration with underlying system

services file system

Macros Unix kernel rewritten for the PDP-11 in C

portable I/O package From 1973 to 1978 ...

more types expanded libraries and environment emphasis on portability

6

Saturday, January 8, 2011

Page 7: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Reasons for its success... Success of Unix It’s quite simple and surprisingly uniform

declarations mimic expressions supports low-level reasoning

• pointers and arrays as index into regions of memory, generalized casts ....

supports high-level reasoning• types and operators well-defined• static compilation

useful set of libraries to enable portability and interaction with a real environment

Extraordinarily stable no major change since 1983

7

Saturday, January 8, 2011

Page 8: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

What’s not to like? Programming model close to the machine

no abstraction• no objects, modules, datatypes, ...

why is abstraction important? No safety

“Well-typed programs don’t go wrong” doesn’t apply here.

Easy to generate security vulnerabilities• buffer overflows, unwanted overwriting of memory

No automatic memory management Is programming mathematics or

engineering?8

Saturday, January 8, 2011

Page 9: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Why learn C, on balance

Power/performance: low-level control over the OS.

Understanding: understand better the interaction between machine and software: C as the expression of a low-level virtual

machine Betterment: gain a better appreciation of

high-level programming languages C is not a panacea “With power comes responsibility”

9

Saturday, January 8, 2011

Page 10: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Requirements

Learning how to program is achieved by DOING the PROJECTS

Mandatory class and lab attendence YOU HAVE TO SUBMIT ALL PROJECTS You will not get a grade unless you submit all

projects (with at least an attempt to solve it)

10

Saturday, January 8, 2011

Page 11: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Course information

Meetings MW:3:30 - 4:20pm, SMTH 108, Jan 10 - Apr. 30

Professor contact info: Office: LWSN 3141J Email: [email protected] Office hours: MW 2:30 - 3:30 or by appointment

TA: TBA

Class webpage http://www.cs.purdue.edu/homes/suresh/cs240-spring2011

11

Saturday, January 8, 2011

Page 12: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Class attendance and taking notes

REQUIRED !!! If you need to miss class, let me know Slides will be made available online

before lecture but YOU ARE STRONGLY RECOMMENDED TO TAKE NOTES

12

Saturday, January 8, 2011

Page 13: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Grading policy

Programming Labs 15% Projects 30%

Exams Midterm 1 15% Midterm 2 15% Final 25%

13

Saturday, January 8, 2011

Page 14: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Lecture 1/ Spring 2010

Reference material

The C Programming Language, Brian W. Kerninghan and Dennis M. Ritchie, 2nd Edition

Lecture slides posted online

14

Saturday, January 8, 2011

Page 15: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Readings for this lecture

Chapter 1: Introduction

15

Saturday, January 8, 2011

Page 16: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Terminology

What’s a computer? What is hardware/software What’s an algorithm ? What’s a program? What’s a library? What’s an operating system? What’s a programming language ?

Machine language Assembly language High-level language

16

Saturday, January 8, 2011

Page 17: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Computer architecture

Memory

CPU

Storage

External I/O Devices

keyboard,mouse

Network

DisplayCPU...

17

Saturday, January 8, 2011

Page 18: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

HARDWARE

OPERATING SYSTEM

APPLICATIONS(Programs)

System Layers

18

Saturday, January 8, 2011

Page 19: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

HARDWARE

OPERATING SYSTEM

APPLICATIONS(Programs)

System Layers

18

Saturday, January 8, 2011

Page 20: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

HARDWARE

OPERATING SYSTEM

APPLICATIONS(Programs)

Software that allows multiple users/app to share efficiently and easily a computer

System Layers

18

Saturday, January 8, 2011

Page 21: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Operating System

Management of the processes and their access to resources Memory CPU access I/O Network Other devices

Interaction with the user Graphic interface Other devices

19

Saturday, January 8, 2011

Page 22: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Algorithm/Program

Algorithm: procedure for solving a problem in finite steps

Program: set of instructions to the CPU, stored in memory, read and executed by the CPU Differentiate between the abstract (algorithm)

and concrete (program source) description of a computation

Differentiate between the concrete description of a computation and its execution (translated machine code) on hardware

20

Saturday, January 8, 2011

Page 23: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

Machine and assembly language

Machine language : binary information, specific to a CPU How a CPU interprets data: e.g. how are memory

addresses represented, how is an instruction coded, etc

This is the binary or executable code Assembly language: easier to write for

people, using symbols, requires an assembler Still need to think in terms of low level CPU steps Still hardware-specific

21

Saturday, January 8, 2011

Page 24: CS240: Programming in C - Purdue University · 2011-01-09 · History The world in 1969 Fortran, PL/1 and Cobol were the primary “high-level” programming languages available

High-level language

Closer to human language How much closer?

Needs a compiler to convert it to machine language Demands precision in its definition

One can write programs in many high-level languages for the same CPU What does this imply about the kinds of computation a

HLL can (or cannot) perform? More portable (more details later)

22

Saturday, January 8, 2011