FEEDBACK: not so much on bytes and masks &c. don't let scanf drag you into a discussion of pointers relate concepts more - e.g. parentheses after main and after printf repeat questions from the audience give a break midway, to allow students to ask questions TUESDAY 5 JANUARY this is the SIPB CCC introduce self biggest class previously was 18 people; please break in w/ questions how many Scheme programmers? Pascal? others? any assembly languages? some people say C is evil: I think of it more as chaotic neutral. C is a lot like MIT: gives a lot of freedom, counts on you to use it wisely by the end of this course, you'll have enough rope to hang yourself getting help: zctl add ccc \* \* zwrite -c ccc -i help BOOKS: Kelley & Pohl, A Book on C Koenig, C Traps and Pitfalls K&R, The C Programming Language others, for Pascal programmers PLEASE DON"T BORROW THEM FOR A LONG TIME notes - won't refer to them, but useful as a 2nd treatment section 3 of the UNIX manual SYLLABUS: http://www.mit.edu/iap/c3/ mirrored at http://www.mattababy.org/~belmonte/Teaching/CCC/ structure of C is more understandable w/ historical context 1967: Martin Richards: BCPL an opposite pole from Algol (and later Pascal) on the PDP-7, everything was a "cell" "close to the machine" - everything is just an integer memory is just a big array this is simple and beautiful, but drives you up the wall when you want to do anything abstract the PDP-7 had something like four kilobytes of memory so BCPL was a small language depends on libraries, not built-ins in fact, in C, there are only 32 reserved words Ken Thompson: B Dennis Ritchie was playing with something called UNIX; invented C added some idea of types to B but types are fudged in Scheme, can you do this: (+ #\A 5) in Pascal, can you do this: 'A' + 5 EVERYTHING'S AN INTEGER, OR COMPOSED OF INTEGERS ASCII fractions in scientific notation even memory addresses! (much more about this later) C is not a strongly typed language hello.c cc -g -o hello hello.c add gcc; gcc -g -o hello hello.c (which to use? a religious question.) comments main (compare Pascal's "program" keyword) return type blocks printf() and stdio string constant \n (otherwise prints continuously - example) return (suppose we left it out?) lint (useful for some of the bugs we're going to talk about) add gnu; gcc -wall fahr.c declarations and statements variables and types - different from Scheme - Scheme uses types implicitly char, int, short, long - typically 1, 2 or 4, 2, 4 float, double - IEEE 4, 8 signed, unsigned modifiers - specify use of MSb note no "string" type - we'll get to that initialisation assignment - = vs. == - compare Pascal statements & exprs - can have a=b=5 can have x++ as stmt or expr comma operator - will see later - for example x++, y++; order of operations format strings - %d %f %c %s, and others %e scientific, %g shorter of float or sci. the call to printf is actually an expr - returns # of chars printed fahr.c, v1 scanf - use reference operator (PITFALL!) returns # of inputs converted fahr.c, v2 doubles are long floats why no \n in prompt? field width specs why %lf in scanf but no 'l' in printf? argument promotion to double or to int fahrcels.c, v0 #include scale is an int not a char, because getchar() can return EOF conditional statement takes Boolean but Boolean is implemented as int logical ops in C produce int 0 or int 1 blocks are compound stmts - like Pascal's "begin" and "end" what would happen if we left out the bottom set of braces? (bad ctrl flow) the top set? (sn. err.) fahrcels.c, v1 another logical op, || on parenthesising Boolean exprs - don't rely on precedence 'cause it'll bite you fahrcels.c, v2 bitwise ops are vector versions of logical ops explain hex numbers - each nybble is a digit remember chars are just numbers could we use logical && here? (NO.) usefulness in reading hardware registers, decoding flags, &c. (ex: PC game port at *(char *)0x201: h,v,x,x,b1,b2,x,b3) what op do I use to read each bit? what mask? another ex: what op do I use to make an even number odd? fahrcels.c, v3 predefined functions in ctype.h - <> - on Athena, /usr/include fahrcels.c, v4 sometimes we want to write a conditional within an expr, inside another stmt (easily abused and obfuscated!) fahrcels.c, v5 shorthand ops - take inspiration from assembly languages fahrcels.c, v6: FIND THE BUGS! WEDNESDAY 6 JANUARY review if/else; go over hw numwords.c: switch (like Pascal's "case") pitfall: terminate each case with a "break" use "default" to catch bugs and error conditions factorial.c: "L" suffix for and while peasant.c: for, with some bitwise ops invariants, guards, bounds get them to read the code and state the algorithm preprocessor.c bignumwords.c: functions - pass parameters on the stack EXPLAIN do-while GIVE OVERVIEW OF INTERPRETER PROJECT go over assignment: scanner.h & scanner.c THURSDAY 7 JANUARY quadratic.c: math library; static data; storage classes in general iteration and recursion scope FRIDAY 8 JANUARY complex.h: a few ways to do this - struct - "a tagless union, structure, or enumeration specifies a unique type" tagged struct and typedef are almost equivalent in functionality why would you want to use a typedef? (to hide implementation as a struct - data abstraction) #defines. note parenthesisation complex.c: call-by-value causes a lot of copying talk about the way parameters are passed on the stack and local (automatic) variables are allocated on the stack they go away when the function returns better to use pointers review RAM model review pointer usage in scanf calls pcomplex.c: string.c: arrays, pointers, and strings (why no & on string var name in scanf?) HOMEWORK: use an array of structs to implement a database TUESDAY 12 JANUARY median.c: multidimensional arrays vs. arrays of pointers; pointers to functions; casts echo.c: argc and argv llist.c, llist.h: if time allows, get the class to write a bubble-sort HOMEWORK: alter the median routine to take a median of keyed structs HOMEWORK: think about the two-dimensional-array version of median.c HOMEWORK: make some of the previous programs take command-line arguments WEDNESDAY 13 JANUARY malloc and free - different from heap allocation in Scheme & Pascal modular construction gdb HOMEWORK: parser.c THURSDAY file i/o - fopen & fclose, fscanf & fprintf, fread & fwrite, ftell & fseek HOMEWORK: modify parser.c to take input from a file; write evaluator.c FRIDAY special topics and catch-up time