CSC195, Class 25: Input and Output Overview: * Character input and output. * Typed input. * Lab Notes: * Happy Seuss-week * How many of you plan to be here for class on the day before break? + Class cancelled day before break. * Questions on homework? * Sam will not be here this Friday. Ben will run lab. * Lindsey plays on Wednesday, extra credit. ---------------------------------------- Basic operations for I/O (all library) * getchar() * putchar() * getc() getc() getc(stdin) * putc() - writes a character to a designated output * ungetc() Writing more interesting types: * printf(PATTERN, VAL1, VAL2, .... ) Reading more interesting types: * scanf(PATTERN, ADDRESS1, ADDRESS2, ...) int i; scanf("%d", &i); Why send values to printf and addresses to scanf? * C's whole "pass by value" style requires it. * And we need space for the result You can put extra stuff in your scanf: scanf("Hello: %d", &i); Reads the current line, expecting it to have the form "Hello: %d" The number gets shoved in i. The hello gets ignored. If the input happened to be "Goodbye", the scanf will fail and the state of the input may not be what you expect. How do you do general-purpose I/O? * Write it yourself using character I/O. Standard character I/O is buffered; hard to do editing ---------------------------------------- What? Test my code? Never. * By default, Unix and C use "buffered" I/O. * Why does getchar() return an int? * What's wrong with the first "figure out input"?