CS/CE 218 Lecture -*- Outline -*- connection: So far we've studied Unix and shell programming. In part this has shown us the environment in which Unix programs are written and "live". We've studied the "shape" of a Unix process, and have already learned many conventions to follow in C programs (exit value, error messages, standard I/O, filters, environment, command line arguments, etc.). We've also learned about several tools that are helpful in writing C programs (e.g., emacs, grep), and in avoiding writing C programs. We've looked at a lot of software tools, and written some of our own. Now we're ready to write more efficient and sophisticated tools in C... This lecture will try to give an overview of C; next time we'll start on details. * Introduction to C Q: Why is C an important language? implementation language for Unix => Important commercial language integrated with other Unix utilities (such as lex and yacc) => used in the compiler course (442) MOHLL -- machine-oriented high level language it's almost like a portable assembly language ** History Q: What other languages inspired C? When did development on B start? CPL (Cambridge, around 1963) BCPL (M. Richards, et al, 1967), typeless systems programming lang. Algol 68 (1968), typed descendent of Algol 60 B (K. Thompson, 1970), also typeless C (Kernighan, Ritchie, 1972) Publication of "The C Programming Language (1st ed) (1978) *** types and type checking to solve technical problems untyped languages e.g., word-oriented typeless langauges such as BCPL and B + vs. .+ for floating point addition addresses vs. integers 4-8 byte floats *** ANSI C (1988) Major changes in function declarations, standardization of library. Q: Is ANSI C a major change from "classic" C? no C is over 20 years old... there are much better languages now for most programming, but perhaps not for systems programming still, few languages as widely accepted as C C is better than several other general purpose langs (FORTRAN, Pascal, Basic) Q: Is C only used on Unix systems? **Goals *** systems programming langauge higher level than assembler efficiency of generated code a concern Q: In what way is C a "low-level" language? manipulates same objects that real machines do (chars, nums, addrs, registers) operations work similarly to real machines shift, increment, etc. all directly expressible Does that make a programming language sound like an ADT? yes... *** small size and simplicity Q: What characteristics help keep C small? not a lot of built-in fancy stuff like g.c., I/O no parallelism, etc built in makes run-time suppport for the language small *** portability, displacing assembly language seems paradoxical, but really want low-level programs to be as portable as possible. Portability is extremely important in commercial programming. *** let programmers say what they want to say "preservation of information" Q: Is the philosophy of C to keep you from making mistakes? no ** Some hints on learning C (better) ---------------------- Keep 3 lists (or highlight specially in the book): 1. tips for writing portable code e.g., printf is part of the ANSI standard (page 11) 2. pragmatic advice e.g., write only one statement per line (page 10) when to use while vs. for (before ex. 1.5 on p. 14) 3. common pitfalls e.g., writing = instead of == (page 19) Do the programming exercises we recommend ---------------------- even the simple ones can be illuminating ** examples Q: Can you write the equivalent shell (or Pascal) program for the following C programs? *** hello world! (section 1.1) Q: What does each line of the following do? ------------------ #include main() { printf("These characters appear on standard output\n"); } ------------------ note: semicolon (;) is a statement terminator can also write: printf("These characters" " appear on standard output\n"); Q: What happens if you leave out the `\n' ? Q: How can you tell whether the characters really appear on standard output? *** variables and arithmetic expressions (section 1.2) ----------------- #include main() /* effect: print lines with values of i, T^(i)(7), for i from 0 to 10 */ { int i, n; n = 7; i = 0; while (i < 11) { printf("%d\t%d\n", i, n); if ((n % 2) == 0) n = n / 2; else n = (3*n + 1) / 2; i++; } } ----------------- note: comments (and problem with it), declarations, assignment statements, while, if % is modulo arithmetic: n % 2 is same as n mod 2 in Pascal i++ is same as i := i + 1 in Pascal output: 0 7 1 11 2 17 3 26 4 13 5 20 6 10 7 5 8 8 9 4 10 2 *** for loops ----------------- #include main() /* effect: print lines with values of i, T^(i)(7), for i from 0 to 10 */ { int i, n; n = 7; for (i = 0; i < 11; i++) { printf("%d\t%d\n", i, n); if ((n % 2) == 0) n = n / 2; else n = (3*n + 1) / 2; } } ----------------- same effect as the previoius version. point out initialization, test, step Q: Can you do exercise 1-5 (on page 14)? *** symbolic constants Q: What does the #define do in the following? ------------------- #include #define HOWMANY 11 main() /* effect: print lines with values of i, T^(i)(7); 0 <= i <= HOWMANY-1 */ { int i, n; n = 7; for (i = 0; i < HOWMANY; i++) { printf("%d\t%d\n", i, n); if ((n % 2) == 0) n = n / 2; else n = (3*n + 1) / 2; } } ------------------- constants usually written in upper case note, can't make n a symbolic constant Q: Should there be a semicolon after 11 in the above program? a better (more modern) alternative: const int howmany = 10+1; *** character I/O (section 1.5) Q: How do you output a character in C? **** copy program (section 1.5.1) Q: Why is the result of getchar an int instead of a char? for EOF not an int Q: Can you do exercise 1-6 on page 17? **** character counting (section 1.5.2) Q: what does ++nc mean? Q: What is a null statement? **** line counting (section 1.5.3) Q: Can you do exercise 1-9 on page 20? **** word counting Q: How does the program on page 20 differ from the wc program? Q: How is || different from ``or'' in Pascal? *** arrays (section 1.6) Q: What's the low bound of an array in C? Q: How do C arrays differ from Pascal arrays? syntax, no bounds checking, start from 0 always Q: Can you do the first part of exercise 1-13 on page 24? *** C is an expression language (bottom of page 16, through page 17) assignments are expressions, value (the value of the rhs) any expression can be used as a statement e.g., main(){ 1; } is a legal program! -------------- #include main() { int n, j, i; printf("%d\n", n = 3); j = i = n; printf("%d\t%d\n", j, i); } -------------- output is: 3 3 3 This is often used in loops: *** functions (sections 1.7-1.8) ---------------- #include int T(int); main() /* effect: print lines with values of T^(i)(7), until T^(i)(7) is 1 */ { int x = 7; do { printf("%d\n", x); } while ( (x = T(x)) > 1 ); return 0; } int T(int n) /* ensures result = if odd(n) then (3n+1)/2 else n/2 */ { if ((n % 2) == 0) n = n / 2; else n = (3*n + 1) / 2; return n; } ---------------- note: extern declaration syntax, idea behind the syntax, return statement, do-while, expression in test. in T, n is a (formal) parameter (or formal argument) in the call T(x), x is the (actual) argument. return in main sets the exit value Note: functions don't nest in C, unlike Pascal this actually helps make C more efficient... **** arguments passed by value Q: Are arguments in C passed like var parameters in Pascal? arguments passed by value: call of the form T(x) does not change x Q: Is it possible to get var parameter like effect? *** strings and character arrays (section 1.7) Q: How does C represent strings? Q: What is the null character used for? Q: Can you do exercise 1-18 on page 31? *** external variables and scope (section 1.10) Q: How can you make variables local to a function? Q: What is an automatic variable? How does auto differ from static? Q: How do you define a "global variable" in C? Q: What's the difference between a definition and a declaration? Q: Can function declarations be made local to the client function? Q: Can you write exercise 1-22 on page 34?