prof. béat hirsbrunner ammar halabi, phd student (exercises) dani rotzetter, master student...

22
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) achelor students : Major in computer science (3rd semester) Minor in computer science Optionnal course MCS Gateway students : Complementary course for the Joint Master in Computer Science Imperative & System Imperative & System Programming Programming IN.3011 – Systemnahe IN.3011 – Systemnahe Programmierung Programmierung IN.3011 – Programmation IN.3011 – Programmation proche proche du du système système University of Fribourg, Department of Informatics Autumn Semester 2012, www.unifr.ch/diuf/pai/ip Prerequisite: Basic knowledge of a programming language, e.g. Java

Upload: noah-tucker

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

Prof. Béat HirsbrunnerAmmar Halabi, PhD student (exercises)Dani Rotzetter, Master student (exercises)

Bachelor students :• Major in computer science (3rd semester)• Minor in computer science• Optionnal course

JMCS Gateway students :• Complementary course for the Joint Master in Computer Science

Imperative & SystemImperative & System

ProgrammingProgrammingIN.3011 – SystemnaheIN.3011 – Systemnahe ProgrammierungProgrammierung

IN.3011 – ProgrammationIN.3011 – Programmation procheproche dudu

systèmesystème

University of Fribourg, Department of InformaticsAutumn Semester 2012, www.unifr.ch/diuf/pai/ip

Prerequisite: Basic knowledge of a programming language, e.g. Java

Page 2: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

#include <stdio.h>

main(){

printf("hello, world\n");}

(KR p6)Imperative and System Programming, B. Hirsbrunner, Lecture 1 – 19 September 2012Course organization: ~30’; Unix Tutorial: ~60’; C Tutorial: ~20’; KR – Chap 1: ~70’

Hello WorldHello World

% gcc hello.c // Compile hello.c% ./a.out // Run a.out

% gcc -o hello hello.c // Compile hello.c% ./hello // Run hello

Page 3: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

3

#include <stdio.h>

/* Print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */main(){

int fahr, celsius;int lower, upper, step;

lower = 0; /* lower limit of temperature table */upper = 300; /* upper limit */step = 20; /* step size */

fahr = lower;while (fahr <= upper) {

celsius = 5 * (fahr - 32) / 9;printf("%d\t%d\n", fahr, celsius);fahr = fahr + step;

}} (KR p9)

Variables and Arithmetic Expressions (1)Variables and Arithmetic Expressions (1)

Page 4: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

4(KR p12)

#include <stdio.h>/* Print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 *//* Floating-point version */main(){

float fahr, celsius;int lower, upper, step;

lower = 0; /* lower limit of temperature table */upper = 300; /* upper limit */step = 20; /* step size */

fahr = lower;while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr - 32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;

}}

Variables and Arithmetic Expressions (2)Variables and Arithmetic Expressions (2)

Page 5: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

5

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

}

(KR p13)

The For StatementThe For Statement

Page 6: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

6

#include <stdio.h>

#define LOWER 0 /* lower limit of table */

#define UPPER 300 /* upper limit */

#define STEP 20 /* step size */

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

}

(KR p15)

Symbolic ConstantsSymbolic Constants

Page 7: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

7

#include <stdio.h>

/* copy input to output; 1st version */main(){ int c;

c = getchar(); while (c != EOF) { putchar(c); c = getchar(); }}

(KR p16)

File Copying (1)File Copying (1)

Page 8: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

8

#include <stdio.h>

/* copy input to output; 2nd version */

main()

{

int c;

while ((c = getchar()) != EOF)

putchar(c);

}

(KR p17)

File Copying (2)File Copying (2)

Page 9: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

9

#include <stdio.h>

/* count characters in input; 1st version */main(){ long nc;

nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc);}

(KR p18)

Character Counting Character Counting (v1)(v1)

Page 10: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

10

#include <stdio.h>

/* count characters in input; 2nd version */main(){ double nc;

for (nc = 0; getchar() != EOF; ++nc) ; printf("%.0f\n", nc);}

(KR p18)

Character Counting Character Counting (v2)(v2)

Page 11: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

11

#include <stdio.h>

/* count lines in input */main(){ long c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl);}

(KR p19)

Line CountingLine Counting

Page 12: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

12(KR p20)

init

S = {‘ ‘, ‘\n’, ‘\t’}

c not in S

c in S

c not in S

c in S

exit exit

c == EOFc == EOF

++nw : for every state transition "out" to "in"

inout

State machine

Word CountingWord Counting

if (c == ' ' || c == '\n' || c == '\t') state = OUT;else if (state == OUT) { state = IN; ++nw;}

/* count lines, words, and characters in input */

while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl;

main(){ int c, nl, nw, nc, state;

state = OUT; nl = nw = nc = 0;

} printf("%d %d %d\n", nl, nw, nc);}

#include <stdio.h>

#define IN 1 /* inside a word */#define OUT 0 /* outside a word */

Page 13: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

13

#include <stdio.h>

/* count digits, white space, others */

main()

{

int c, i, nwhite, nother;

int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)

ndigit[i] = 0;

(KR p22)

ArraysArrays (1 / declaration + initialization)(1 / declaration + initialization)

0 1 2 3 4 5 6 7 8 9index

ndigit ndigit[2]

Page 14: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

14

while ((c = getchar()) != EOF)

if (c >= '0' && c <= '9')

++ndigit[c - '0'];

else if (c == ' ' || c == '\n' || c == '\t')

++nwhite;

else

++nother;

printf("digit =");

for (i = 0; i < 10; ++i)

printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n",nwhite,nother);

}

(KR p22)

ArraysArrays (2 / algo + output)(2 / algo + output)

// c - '0': See ASCII table !

Page 15: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

15

#include <stdio.h>

int power(int m, int n); // power: m,n m**n

/* test power function */main(){

int i;

for (i = 0; i < 10; ++i)printf("%d %d %d\n", i, power(2, i), power(-3,

i));return 0;

}

(KR p24)

FunctionsFunctions (1 / main program)(1 / main program)

Page 16: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

16

/* power: raise base to

n-th power; n >= 0 */

int power(int base, int n)

{

int i, p;

p = 1;

for (i = 1; i <= n; ++i)

p = p * base;

return p;

}

(KR p24)

power (base, n) = basen

FunctionsFunctions (2 / power function)(2 / power function)

Function call Diagram

= base * base * … * base (n times)

Page 17: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

17

/* power: raise base to

n-th power; n >= 0;

version 2 */

int power(int base, int n)

{

int p;

for (p = 1; n > 0; --n)

p = p * base;

return p;

}

(KR p27)

Arguments – Call by ValueArguments – Call by Value

Function call Diagram

power (base, n) = basen

= base * base * … * base (n times)

Page 18: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

18

#include <stdio.h>#define MAXLINE 1000 /* maximum input line size */

int getline(char line[], int maxline);void copy(char to[], char from[]);

/* print longest input line */main(){

int len; /* current line length */int max; /* maximum length seen so far */char line[MAXLINE]; /* current input line */char longest[MAXLINE]; /* longest line saved here */

max = 0;while ((len = getline(line, MAXLINE)) > 0)

if (len > max) {max = len;copy(longest, line);

}if (max > 0) /* there was a line */

printf("%s", longest);return 0;

} (KR p29)

Character ArraysCharacter Arrays (1 / main)(1 / main)

Page 19: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

19

/* getline: read a line into s, return length */int getline(char s[], int lim){

int c, i;

for (i = 0; i < lim-1 && (c = getchar())!= EOF && c!='\n'; ++i)s[i] = c;

if (c == '\n') {s[i] = c;++i;

}s[i] = '\0';return i;

}

/* copy: copy 'from' into 'to'; assume 'to' is big enough */void copy(char to[], char from[]){

int i;

i = 0;while ((to[i] = from[i]) != '\0')++i;

} (KR p29)

Character ArraysCharacter Arrays (2 / getline + copy)(2 / getline + copy)

Page 20: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

20

char s[7] = “Hello”;

(KR p30)

C StringC String

In C, a string is stored as an array of characters:

• containing the characters of the string and• terminated with a ‘\0’ to mark the end

Typical navigation

for (i=0; s[i]!=‘\0’; ++i) { // Do some actions}

Page 21: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

21(KR p31)

Scope of the variablesScope of the variables

Function Call Diagram

int i=2; // global variable

int f(int x) { int j=3; // local variable return i*j*x; }

main() { int j=4, r1, r2; // local variables r1 = f(j); r2 = f(5*j);}

• A variable v1 declared inside any

function, inclusive main(), is only known in that function, i.e. no other function can have direct access to it.

• A variable v2 declared outside any function is known and accessible everywhere.

• v1 is called a local or private variable, and v2 a global or public variable.

Page 22: Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd

22

#include name#define name replacement text

main(){

}

while (expression)statement

for (expr1; expr2; expr3)statement

if (expr1)statement1

else if (expr2)statement2

…else

statement_n

Return-type function-name (parameter declarations, if any)

{

declarations

statements

}

SummarySummary

Definition. A statement is an expression followed by a semicolon or a sequence of 'expression;' surrounded by { }