21
Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: [email protected] Two instances: 1. Tuesdays 15:00-16:00; Kitot 206 2. Wednesdays 11:00-12:00; Kitot 206 Reception: Wednesdays 12:00-13:00 - room #32 at the nano center (under the library)

Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: [email protected] Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Introduction to Systems Programming - Recitation

Omer Kotlicki E-Mail: [email protected]

Two instances: 1. Tuesdays 15:00-16:00; Kitot 2062. Wednesdays 11:00-12:00; Kitot 206

Reception: Wednesdays 12:00-13:00 - room #32 at the nano center

(under the library)

Page 2: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Avishai WoolLecture 1 - 2

Administrivia

Course home page:•http://www.eng.tau.ac.il/~isp

Course Mailing list:•http://listserv.tau.ac.il/archives/eng0512-4402-01.html

Books•Modern Operating Systems, 3rd edition, by Tanenbaum. Prentice-

Hall, 2008. 2nd edition (2001) is fine too.

Grades•Final exam 65%

•Exercises 35%

Page 3: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Avishai WoolLecture 1 - 3

Administrivia – cont.

Significant programming in C

3-4 exercises + final project

Windows programming environment (Microsoft VC++ / Visual Studio)

Assignments in pairs

Sharing / copying exercises will NOT be tolerated

Page 4: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

About the Crash Course

Created by Prof. Cooperstock from McGill University, MontrealCover sufficient C for simple programs:

•variables and statements•control•functions•arrays and strings•pointers

Slides and captured lecture (video and sound) are available at:

http://www.cim.mcgill.ca/~jer/courses/C/

Page 5: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Why C?

C is not always a good choice ...•Safety-critical systems

•Component-based programming / RAD

•...

... but in many applications you do want C•Legacy systems

•Speed, memory requirements

•Low-level programming (close to the machine)

•...

Page 6: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Your First C Program

•"The only way to learn a new programming language is by writing programs in it" [K&R]

/* A simple program that prints something */

#include <stdio.h>

main (){ printf ("Hello, world!\n");}

Page 7: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Declarations

Example:float x;double d = 5;int *p, i, a[100];char s[21];

Syntax:type variable_name, ... [= value];

Rules:•declarations must precede executable statements• int type may be modified by: long, short, unsigned

Page 8: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Changing Variable Values

Example:int x, y, z;x = 2;x = x + 1;

Getting Fancyy = z = 4 + 5;x += 1;++x;x++;y = x--;

Note:•assignment statements return value, which may or may

not be ignored; same goes for increment statements

Page 9: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Formatted Output

Example:int i = 10;float f = 2.5;char s [] = "hi";printf ("Jack\'s integer is %d\n", i);printf ("Jill\0x27s float is %f\n", f);printf ("My string s = %s\n", s);

Syntax:printf (string_with_formatting, var1, var2, ...);

•Formats: %d integer, %f float, %c character, %s string, …•Note "escape sequences": \n newline, \' quote, \0x27, etc.• #include <stdio.h> is compulsory; more about it later

Page 10: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Formatted Input

Example:#include <stdio.h>int i;float f;scanf ("%d %f\n", &i, &f); /* inputs an integer and a float */

Syntax:scanf (string_with_formatting, &var1, &var2,...);

Note:•The ampersand (&) is necessary because scanf modifies the

values stored in the respective variables; by comparison, printf only uses the values, without modifying them. More about this later

Page 11: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

I/O Example

What does this print?#include <stdio.h>

main (){ int n; float x; char mark; scanf ("%d %f %c", &n, &x, &mark); printf ("Of %d %s,\n%f got %c\’s\n", n, "students", x, mark);}

•Type in the following input: 86 85.999 A

Page 12: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

I/O Example

#include <stdio.h>

main (){ int n; float x; char mark; scanf ("%d %f %c", &n, &x, &mark); printf ("Of %d %s,\n%f got %c\’s\n", n, "students", x, mark);}

•Input: 86 85.999 A

•Output: Of 86 students,85.999001 got A's

Page 13: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Conditional Statements

Example:if (age < 0) {

printf ("warning: negative age\n"); age = -age;

}

Syntax:if (condition) statementif (condition) statement else statement

Rules:•the condition is an int ! (no booleans)

•parentheses required around conditional expression

•use curly braces to make a compound statement

Page 14: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

More Conditionals

Example:if (x < 0) printf ("x is less than 0\n");else if (x == 0) printf ("x is equal to 0\n");else printf ("x is greater than 0\n");

What’s wrong with this?if (x < 0) if (y < z) printf ("y is less than z\n");else printf ("x not less than 0\n");

Page 15: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

While Loops

Example:/* print "hi" forever */while (1) printf ("hi");

Syntax:while (condition) statement

Rules (again):•the condition is an int ! (no booleans)

•parentheses required around conditional expression

•use curly braces to make a compound statement

Page 16: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

For Loops

Example:/* print "hi" three times */int i; /* i continues to exist when loop ends */for (i = 0; i < 3 ; i++) printf ("hi");

Syntax:for (statement1; condition; statement2) statement3;

Equivalent to:statement1;while (condition) { statement3; statement2;}

Page 17: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Loop Example

/* print squares up to 100 */main ( ){ int j, up = 100; for (j = 0; j * j <= up; j++) printf ("%d \n", j * j);}

Note:•can’t do: for (int j = 0; ...

•waste of one multiplication per iteration

•can you make it more efficient?

Page 18: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Example (cont’d)

/* print squares up to 100 */void main ( ){ int j, up = 100, sq; for (j = 0; (sq = j * j) <= up; j++) printf ("%d \n", sq);}

Note:•recall equivalence to a while loop: condition is evaluated

before the loop body

Page 19: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Arrays

int years[45];float temperatures [11];void main () { years[0] = 2000; temperatures[11] = -45.67;}

Rules:•indices start at zero

•maximum valid index is the size of the array minus 1

•but C lets you go beyond the declared boundaries

• temperatures[11] is illegal

Page 20: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Characters

Characterschar a, b, c1, c2;a = '0'; b = '\037'; c1 = 'K'; c2 = c1 + 1;

•Assigns values: 48, 31, 75, 76•The sequences '0',...,'9', 'a',...,'z', 'A',...,'Z'

contain characters numbered consecutively

Castingprintf ("%c %d\n", c1, (int) c1);

•Outputs: K 75

Page 21: Introduction to Systems Programming - Recitation Omer Kotlicki E-Mail: omerkot@gmail.com Two instances: 1.Tuesdays 15:00-16:00; Kitot 206 2.Wednesdays

Strings

Strings are '\0'-terminated arrays of char :char s[3] = "hi"; /* invisible '\0' */char t[3] = {'h', 'i', '\0'};

String operations#include <string.h>strlen ("there"); /* returns 5 */strcpy (s, t); /* copy t to s */strcmp (s, t) /* alphabetical comparison */