====================================== Notes for Lecture 4 - January 24, 2008 ====================================== * User interaction ** User input passes through three stages *** Line buffer Characters are saved in a hidden line buffer until Enter (return) is pressed. They are unavailable to the program, and any attempt to read them will block (just as if they had never been typed). *** Input buffer After Enter is pressed, any characters in the line buffer are appended to the input buffer. A new line character is placed at the end of the input buffer. Characters reside in the input buffer until requested by program. *** Program input **** Peeking and reading The program can peek at the first character of the input buffer but leave it there, or it can read the character, which moves it from the input buffer into the program. **** Scanf() operation scanf("%i", &num) operates as follows: ***** Skip whitespace First it skips over all whitespace characters until it comes to a non-whitespace character. It does this by peeking at the next character of the input buffer and testing if it a whitespace character such as space, tab, or new line. If it is, it reads it and peeks at the next, etc. If the input buffer becomes empty, scanf() waits until the user hits Enter again. **** Verify that first non-whitespace character can start a number scanf() checks that the non-whitespace character that stopped the whitespace scan can start an integer. If so, it collects it and begins accumulating the digits of a number. If not, it returns 0, indicating that no number was successfully read. Characters that can start an integer are '+', '-', or a digit '0', ..., '9'. **** Accumulate rest of number scanf() peeks and reads successive digits in turn until it encounters a character that cannot continue a number. At that point it stops reading characters(without reading the stopping character), converts the accumulated characters to an integer, stores that number in the corresponding argument (num in this example), and returns 1. Characters that can continue a number just the digits '0', ..., '9'. ** Observed behavior confusing when input buffer is not empty at start See demos_04/1_readpair for an example of this situation. * Makefiles ** Specify how to compile and link your program ** CC tells which compiler to use ** CFLAGS tells which compiler options to use ** Link rule tells how to build the executable ** Dependencies tell when it is necessary to rebuild a file * C Variables ** Variable has two parts: *** Storage allocation where value resides *** Type **** Tells how many bytes of storage the variable needs **** Tells how to interpret those storage bytes ** Basic integer types ** Signed types *** signed char *** short int *** int *** long int *** long long int ** Unsigned types *** unsigned char *** unsigned short int *** unsigned int *** unsigned long int *** unsigned long long int ** Values *** Unsigned b bits long stores integers between 0 and 2^b - 1. *** Signed b bits long stores integers between -2^{b-1} and 2^{b-1} - 1 ** Conversions *** Semantics **** Values are converted from one type to another as needed **** Value is preserved if target type can represent the value **** Converstion to an unsigned type is always defined The value is taken mod 2^{length of unsigned type in bits}. **** All other conversions are undefined. *** Implementation **** If length of source and target are same, bits are just copied. **** If source is longer than target, rightmost bits are copied. **** If source is shorter than target: ***** Source is sign-extended (if signed type) or 0-filled (if unsigned type) ***** Bits are then copied **** Example: To store signed number 1101 into 8-bit target: ***** Sign extend: 1101 -> 11111101 ***** Store: Copy 11111101 to target **** Example: To store unsigned number 1101 into 8-bit target: **** 0-fill: 1101 -> 00001101 **** Store: Copy 00001101 to target *** [See Handout 04 "Integer Types in C" for further information.]