COMP26120: Algorithms and Imperative Programming Lecture C1: - - PowerPoint PPT Presentation

comp26120 algorithms and imperative programming
SMART_READER_LITE
LIVE PREVIEW

COMP26120: Algorithms and Imperative Programming Lecture C1: - - PowerPoint PPT Presentation

COMP26120: Algorithms and Imperative Programming Lecture C1: Introduction to C Pete Jinks School of Computer Science, University of Manchester Autumn 2011 COMP26120 Lecture C1 1/25 Review Course-unit Contents: Algorithms C


slide-1
SLIDE 1

COMP26120: Algorithms and Imperative Programming

Lecture C1: Introduction to C Pete Jinks

School of Computer Science, University of Manchester

Autumn 2011

COMP26120 Lecture C1 1/25

slide-2
SLIDE 2

Review

Course-unit Contents: – Algorithms – C programming Course-unit Approach: – Active Learning – Reading

COMP26120 Lecture C1 2/25

slide-3
SLIDE 3

Review

Course-unit Contents: – Algorithms – C programming Course-unit Approach: – Active Learning – Reading

COMP26120 Lecture C1 2/25

slide-4
SLIDE 4

Lecture Outline

C Resources Comparing Java and C Input/Output

COMP26120 Lecture C1 3/25

slide-5
SLIDE 5

Lecture C1: You are here

C Resources Comparing Java and C Input/Output

COMP26120 Lecture C1 C 4/25

slide-6
SLIDE 6

Motivation

New Programming Language Paradigm Concepts Useful for certain applications – uncluttered view of hardware (“high-level assembly-code”) – performance CV

COMP26120 Lecture C1 C 5/25

slide-7
SLIDE 7

Motivation

New Programming Language Paradigm Concepts Useful for certain applications – uncluttered view of hardware (“high-level assembly-code”) – performance CV

COMP26120 Lecture C1 C 5/25

slide-8
SLIDE 8

Motivation

New Programming Language Paradigm Concepts Useful for certain applications – uncluttered view of hardware (“high-level assembly-code”) – performance CV

COMP26120 Lecture C1 C 5/25

slide-9
SLIDE 9

A little history

Originally from late 60’s - published 1972 1989/1990 ISO standard (C89, C90) 1999/2000 ISO standard (C99) 201? (C1X)

COMP26120 Lecture C1 C 6/25

slide-10
SLIDE 10

Lecture C1: You are here

C Resources Comparing Java and C Input/Output

COMP26120 Lecture C1 Resources 7/25

slide-11
SLIDE 11

Online Resources

Starting points: Syllabus: http://www.cs.manchester.ac.uk/undergraduate/ programmes/courseunits/syllabus.php?code=COMP26120 Course-unit: http://www.cs.manchester.ac.uk/ugt/COMP26120/ QandA: http://qanda.cs.man.ac.uk/questions/3842/ resources-for-learning-c (tag: comp261-algorithms+c) Point to: – Online C course in Moodle – Online “Standard C” book (Plauger & Brodie) – etc.

COMP26120 Lecture C1 Resources 8/25

slide-12
SLIDE 12

Online Resources

Starting points: Syllabus: http://www.cs.manchester.ac.uk/undergraduate/ programmes/courseunits/syllabus.php?code=COMP26120 Course-unit: http://www.cs.manchester.ac.uk/ugt/COMP26120/ QandA: http://qanda.cs.man.ac.uk/questions/3842/ resources-for-learning-c (tag: comp261-algorithms+c) Point to: – Online C course in Moodle – Online “Standard C” book (Plauger & Brodie) – etc.

COMP26120 Lecture C1 Resources 8/25

slide-13
SLIDE 13

More Resources

man pages etc. Book list on syllabus:

  • The C Programming Language (Kernighan & Ritchie)
  • C: A Reference Manual (Harbison & Steele)
  • Expert C Programming (van der Linden)

COMP26120 Lecture C1 Resources 9/25

slide-14
SLIDE 14

Online C course

http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs

COMP26120 Lecture C1 Resources 10/25

slide-15
SLIDE 15

Online C course

http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs

COMP26120 Lecture C1 Resources 10/25

slide-16
SLIDE 16

Online C course

http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs

COMP26120 Lecture C1 Resources 10/25

slide-17
SLIDE 17

Online C course

http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs

COMP26120 Lecture C1 Resources 10/25

slide-18
SLIDE 18

Online C course

http://moodle.cs.man.ac.uk/file.php/28/coursedata/c cbt/ frontpage.html Navigation: begin return map index Themes : – Java to C – Information Representation – Control Flow – Program Structuring – Input & Output Colour-coding: green, yellow, orange, red Dependencies/Routes (arrows) e.g. use for C labs

COMP26120 Lecture C1 Resources 10/25

slide-19
SLIDE 19

Lecture C1: You are here

C Resources Comparing Java and C Input/Output

COMP26120 Lecture C1 Comparing Java and C 11/25

slide-20
SLIDE 20

Assumptions

You have successfully completed 1st-year Java (or equivalent): – you know how to design non-trivial programmes – you know how to encode your designs using Java You are going to learn C based on this C and Java have a lot of similarities, and a lot of differences Look at programming concepts, saying for each whether C is – similar to Java (but ...) – different from Java (as follows ...)

COMP26120 Lecture C1 Comparing Java and C 12/25

slide-21
SLIDE 21

Assumptions

You have successfully completed 1st-year Java (or equivalent): – you know how to design non-trivial programmes – you know how to encode your designs using Java You are going to learn C based on this C and Java have a lot of similarities, and a lot of differences Look at programming concepts, saying for each whether C is – similar to Java (but ...) – different from Java (as follows ...)

COMP26120 Lecture C1 Comparing Java and C 12/25

slide-22
SLIDE 22

SalaryAnalysis

You should have seen the Java version (or something very similar) last year. Compare the Java and C versions Check center character: “ ” = same in both “|” = in both, but different “<” = only in .java “>” = only in .c Start making a list of similarities and differences. Please work with your neighbour(s).

COMP26120 Lecture C1 Comparing Java and C 13/25

slide-23
SLIDE 23

Lecture C1: You are here

C Resources Comparing Java and C Input/Output

COMP26120 Lecture C1 Input/Output 14/25

slide-24
SLIDE 24

Streams

Moodle theme: Input and Output man stdio etc. Built-in: System.in → stdin System.out → stdout System.err → stderr e.g. System.out.print(...) → fprintf(stdout, ...)

COMP26120 Lecture C1 Input/Output 15/25

slide-25
SLIDE 25

Streams

Moodle theme: Input and Output man stdio etc. Built-in: System.in → stdin System.out → stdout System.err → stderr e.g. System.out.print(...) → fprintf(stdout, ...)

COMP26120 Lecture C1 Input/Output 15/25

slide-26
SLIDE 26

Streams

Moodle theme: Input and Output man stdio etc. Built-in: System.in → stdin System.out → stdout System.err → stderr e.g. System.out.print(...) → fprintf(stdout, ...)

COMP26120 Lecture C1 Input/Output 15/25

slide-27
SLIDE 27

Creating your own Streams

Moodle: Input and Output Streams, Manipulating Streams import java.util.Scanner; import java.io.File; → #include <stdio.h> #include <stdlib.h> Scanner sScan = new Scanner(new File(filen)); → FILE *stream= fopen(filen, "r"); if (!stream) { fprintf(stderr, "can’t open %s\n", filen); exit(-1); } ? → fclose(stream);

COMP26120 Lecture C1 Input/Output 16/25

slide-28
SLIDE 28

Creating your own Streams

Moodle: Input and Output Streams, Manipulating Streams import java.util.Scanner; import java.io.File; → #include <stdio.h> #include <stdlib.h> Scanner sScan = new Scanner(new File(filen)); → FILE *stream= fopen(filen, "r"); if (!stream) { fprintf(stderr, "can’t open %s\n", filen); exit(-1); } ? → fclose(stream);

COMP26120 Lecture C1 Input/Output 16/25

slide-29
SLIDE 29

Creating your own Streams

Moodle: Input and Output Streams, Manipulating Streams import java.util.Scanner; import java.io.File; → #include <stdio.h> #include <stdlib.h> Scanner sScan = new Scanner(new File(filen)); → FILE *stream= fopen(filen, "r"); if (!stream) { fprintf(stderr, "can’t open %s\n", filen); exit(-1); } ? → fclose(stream);

COMP26120 Lecture C1 Input/Output 16/25

slide-30
SLIDE 30

Writing characters

Moodle: Character Manipulation, Revisited putchar(ch) – character fputc(stream, ch) putchar(ch) is fputc(stdout,ch) printf(...) – formatted fprintf(stream, ...) sprintf(string, ...) printf(...) is fprintf(stdout, ...)

COMP26120 Lecture C1 Input/Output 17/25

slide-31
SLIDE 31

Writing characters

Moodle: Character Manipulation, Revisited putchar(ch) – character fputc(stream, ch) putchar(ch) is fputc(stdout,ch) printf(...) – formatted fprintf(stream, ...) sprintf(string, ...) printf(...) is fprintf(stdout, ...)

COMP26120 Lecture C1 Input/Output 17/25

slide-32
SLIDE 32

printf

Moodle: (Reading and) Writing System.out.print (String.format(...)); → printf (...); ("%2d earns %5d, which is %5d %s", i, j, k, s) println("...", ...) → printf("...\n", ...) System.out.println("mean is:\t" + mSal); System.out.println("rounds to:\t" + mSalR); → printf("mean is:\t%f\n" "rounds to:\t%d\n", mSal, mSalR);

COMP26120 Lecture C1 Input/Output 18/25

slide-33
SLIDE 33

printf

Moodle: (Reading and) Writing System.out.print (String.format(...)); → printf (...); ("%2d earns %5d, which is %5d %s", i, j, k, s) println("...", ...) → printf("...\n", ...) System.out.println("mean is:\t" + mSal); System.out.println("rounds to:\t" + mSalR); → printf("mean is:\t%f\n" "rounds to:\t%d\n", mSal, mSalR);

COMP26120 Lecture C1 Input/Output 18/25

slide-34
SLIDE 34

printf

Moodle: (Reading and) Writing System.out.print (String.format(...)); → printf (...); ("%2d earns %5d, which is %5d %s", i, j, k, s) println("...", ...) → printf("...\n", ...) System.out.println("mean is:\t" + mSal); System.out.println("rounds to:\t" + mSalR); → printf("mean is:\t%f\n" "rounds to:\t%d\n", mSal, mSalR);

COMP26120 Lecture C1 Input/Output 18/25

slide-35
SLIDE 35

printf

Moodle: (Reading and) Writing System.out.print (String.format(...)); → printf (...); ("%2d earns %5d, which is %5d %s", i, j, k, s) println("...", ...) → printf("...\n", ...) System.out.println("mean is:\t" + mSal); System.out.println("rounds to:\t" + mSalR); → printf("mean is:\t%f\n" "rounds to:\t%d\n", mSal, mSalR);

COMP26120 Lecture C1 Input/Output 18/25

slide-36
SLIDE 36

Reading characters

Moodle: Reading (and Writing), Revisited while (salariesScanner.hasNextLine()) String data = salariesScanner.nextLine(); → enum {buffer size = 100}; char data[buffer size]; while (fgets(data, buffer size, stream)) int currentsalary = Integer.parseInt(data); → int currentsalary = atoi(data);

COMP26120 Lecture C1 Input/Output 19/25

slide-37
SLIDE 37

Reading characters

Moodle: Reading (and Writing), Revisited while (salariesScanner.hasNextLine()) String data = salariesScanner.nextLine(); → enum {buffer size = 100}; char data[buffer size]; while (fgets(data, buffer size, stream)) int currentsalary = Integer.parseInt(data); → int currentsalary = atoi(data);

COMP26120 Lecture C1 Input/Output 19/25

slide-38
SLIDE 38

scanf

ch=getchar() – character ch=fgetc(stream) getchar() is fgetc(stdin) scanf(...) – formatted fscanf(stream, ...) sscanf(string, ...) scanf(...) is fscanf(stdin, ...)

COMP26120 Lecture C1 Input/Output 20/25

slide-39
SLIDE 39

scanf

ch=getchar() – character ch=fgetc(stream) getchar() is fgetc(stdin) scanf(...) – formatted fscanf(stream, ...) sscanf(string, ...) scanf(...) is fscanf(stdin, ...)

COMP26120 Lecture C1 Input/Output 20/25

slide-40
SLIDE 40

Reading a line of input

#define __USE_BSD #include <string.h> char * nextline (FILE* stream) { char data [buffer_size]; fgets (data, buffer_size, stream);//test result int len= strlen (data); data[len-1]= ’\0’; //test was ’\n’ char* temp= strdup (data); //test result return temp; }

COMP26120 Lecture C1 Input/Output 22/25

slide-41
SLIDE 41

Reading fields from an input line

char one[buffer_size], two[buffer_size], three[buffer_size]; sscanf(data, "%s %s %s", one, two, three); Result of sscanf = number of things read or EOF

COMP26120 Lecture C1 Input/Output 24/25

slide-42
SLIDE 42

Reading fields from an input line

char one[buffer_size], two[buffer_size], three[buffer_size]; sscanf(data, "%s %s %s", one, two, three); Result of sscanf = number of things read or EOF

COMP26120 Lecture C1 Input/Output 24/25

slide-43
SLIDE 43

Lecture Review

C Resources

COMP26120 Lecture C1 Input/Output 25/25