Lecture notes for CSC 173, Thurs. Sept. 19 ------------------------------------------------- READING ASSIGNMENT: your choice. One possibility is Barrett & Wagner 91-95, 108-126, 149-150, 173-183, 199-208, 254-283, and 153-163, 283-293, 426-436. ------------------------------------------------- ======================================================================== C First: Why study C? - lang of choice for system programming e.g., most OSs, language runtime systems (JVM) - generally fast, simple implementations - programmer control over impl. details - used in many upper level CS courses Why C in 173? - not related to formal systems - need to cover it somewhere -------------- Note: presentation oriented toward knowing Java focus mostly on differences -------------- function calls (no methods); parameter modes no implicit this parameter; no object name-dot everything except arrays passed by value arrays passed by reference; interchangable with pointers (more on this later) program starts in main, with argc, argv arguments: int main (int argc, char *argv[]) { ... argc - number of command-line arguments. argv - command line argv[0] - program name argv[argc] - last argument. ---------------- storage classes and separate compilation (global) static -- accessible from any file -- exist for duration of program execution (actually, even without static, global vars do this) (local) static -- accessible only within a scope -- exist for duration of program execution extern -- allows access to vars in other files -- no space is allocated const #include (e.g., #include stdio.h) typedefs and externs in .h file matching globals in *one* .c file each I/O #include printf, scanf, puts, gets, putchar, getchar FILE *fopen (const char* filename, const char *mode) fprintf, fscanf, fputs, fgets, fputc, fputc, fwrite, fread also sprintf, sscanf types structs are like classes without methods, and with all-public fields declaration syntax read right as far as possible, then left, then out one level of parentheses (if any) and repeat parentheses appear mainly in really messy declarations involving function pointers; you probably won't see any in this class struct foo *bar[10]; bar is a 10-element array of pointers to foo structs const char *s; s is a pointer to chars that are constant v. char * const t; t is a constant pointer to chars typedef, struct names typedef struct fraction { typedef struct { int numerator; int numerator; int denominator; int denominator; } FRACTION } fraction; FRACTION myFrac; fraction myFrac; without typedef: struct fraction myFrac; pointers v. refs; in-line allocation of structured objects This is one of the potentially most confusing differences between Java and C. In Java everything that isn't of a basic built-in type (i.e. all classes and all arrays) is dynamically allocated. A variable of a non-basic type is a *reference* to the dynamically-allocated space, so it's perfectly natural to have recursive types in Java: class listnode { mytype data; listnode next; } In C all types (including structs and arrays) are allocated in place unless you do something special to force dynamic allocation. This means recursive types don't make sense. You need an explicit POINTER type: struct listnode { mytype data; struct listnode * next; } example: search for element in list struct node { int datum; struct node *next; } node *find (int d, node *head) { while (head) { if (head->datum == d) /* could also say (*head).datum */ return head; head = nead->next; } return 0; }