SIPB IAP 2001Crash Course in C |
Leader:
Matthew Belmonte <sipb-iap-c3@mit.edu>
Lecturers: Matthew Belmonte, Nick Ingolia, Alex Rolfe
Description:
A 2-week intensive introduction to C for people with some prior programming experience (e.g. 6.001).
Class Schedule:
Weekdays, Tuesday 16 January through Thursday 25 January (8 meetings), 9pm-11pm
Meeting Place:
3-133
Suggested Background or Preparation:
Some programming experience (e.g. 6.001).
This is a two-week course covering the basics of the C programming language. It is not a course about algorithms; participants should already be comfortable programming in a language other than C. Using many examples and focussing on common pitfalls, we'll highlight the features and quirks of the C language. By the end of it, you should be comfortable exploring C on your own and will know where to look for more information.
The MIT libraries will allow you to borrow some of these books for many weeks. Since there are many people in this course and few copies of the books, please don't do that. Instead, please try to use them in the libraries, or to borrow them only overnight. Also, to avoid delays in transit, if you borrow them please return them to their libraries of origin.
The easiest way to find someone to help answer your questions is to ask on the ccc zephyr class. You should subscribe to this class, by typing the following command:
zctl add ccc \* \*
To send a message to the class, zwrite to class (-c) ccc with an instance (-i) appropriate to the topic. Here is an example:
zwrite -c ccc -i printf
Type your message now. End with control-D or a dot on a line by itself.
What is the format specifier for a decimal integer?
^D
Please try to look up an answer before asking the class, but if you're really stuck, please do ask. If someone asks a question to which you know the answer, please take the time to reply, on class ccc.
DAY | TOPICS AND EXAMPLES | ASSIGNMENTS | PITFALLS |
---|---|---|---|
Tuesday 16 January (belmonte) |
History: BCPL, B, and C. The imperative paradigm: assignment versus function application. Weak typing and "closeness to the machine". Program structure (hello.c). Atomic types. Statements, expressions, and operators. Formatted input and output: printf and scanf (fahr.c). if...else. Bitwise and logical operators, shorthand assignment operators (fahrcels.c). | Write a program that asks for the user's initials (two characters) and age (an integer), and prints a personalised message stating what the person's age will be next year. The message should be different for odd ages than it is for even ages. What happens when you try to compile a program without a main function? Why do you think the error occurs where it does? |
Assignment is not equality. scanf takes a pointer. Bitwise operators aren't logical operators. Avoid dangling else clauses. |
Wednesday 17 January (belmonte) |
switch (numwords.c). Iteration: for, while, and do... while (factorial.c, peasant.c). The preprocessor (preprocessor.c). Functions (bignumwords.c). | Complete the program scanner.c, which uses definitions from scanner.h. | Terminate cases with breaks. Use longs to avoid overflow. Comment loops with invariants. Don't follow for or while with a semicolon unless you mean it. In a macro definiton, parenthesise everything, and avoid side effects. |
Thursday 18 January (arolfe) |
The math library (quadratic.c). Storage classes and static allocation. Scope. Iteration and recursion. | (from jcyang) Write an iterative and recursive version of factorial which takes an integer n!. Write a function (iterative or recursive) to raise a number to an integer power, x_to_int_n(x,n). |
Return types default to int, so remember to include header files. |
Friday 19 January (belmonte) |
structs and data abstraction (complex.h and complex.c). Pointers (pcomplex.h and pcomplex.c). Arrays, pointers, and strings (string.c). | Write a program that manages a very small database in an array of structs. The program should allow users to insert a record, to delete a record, and to look up the record(s) associated with some index field. (The fields in the database can be whatever suits your fancy.) | To define a self-referential pointer in a struct, use a tag and not a typedef. Iteratively constructed strings must be terminated. Don't return a pointer to a local auto. Avoid the possibility of buffer overruns. |
Monday 22 January (belmonte) |
Multidimensional arrays versus arrays of pointers; pointers to functions; casts (median.c). argc and argv (echo.c). | Alter median.c so that it takes a median of keyed records implemented in structs. How difficult would it be to alter median.c to work with a true two-dimensional array instead of an array of pointers? Alter some of your previous programs so that they take their arguments on the command line instead of prompting for them. | Pointers aren't (always) arrays. |
Tuesday 23 January (ingolia) |
Dynamic allocation the hands-on way: malloc and free (llist.h and llist.c). Modular construction of large programs (llist_driver.c). The dbx debugger. make and makefiles. | Complete parser.c, which uses definitions from scanner.h and parser.h. | Compile with -g. Watch for memory leaks. |
Wednesday 24 January (ingolia) |
File I/O: fopen and fclose, fscanf and fprintf, fread and fwrite, ftell and fseek. | Write evaluator.c. Modify it and scanner.c to take input from files. | |
Thursday 25 January (belmonte) |
Special topics and catch-up time. |
Completed project files (try not to peek): scanner.h, scanner.c, parser.h, parser.c, evaluator.c, Makefile.
Project test routines: testscan.c, testparse.c.
Extended project (To access this, use "cs1" as a username and the numeric portion of the Athena cluster combination as a password).