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
22
Embed
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
#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
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)
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)
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)
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)
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[]){