Types (Chapter 7) Integer types short unsigned short int unsigned int long unsigned long Floating types: floating point number = (sign) mantissa x 2^{exponent} floating types: float single precision floating-point double double precision floating-point long double extended-precision floating-point Charater types char The sizeof operator allows a program to determine how much memory is required to store values of a particular type program: sizeof.c The range of positve numbers of types int and double program: posnumrange.c Binary number system: 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 .... 1 byte = 8 bits Inaccuracies underflow, overflow, rounding error program inaccuracy.c shows that 0.1 cannot be represented exactly, for example Type conversion: Automatic conversion of data types example: int k = 5, m=4, n; double x = 1.5, y = 2.1; z; k + x: the value is 6.5 z = k / m; expression value is 1, value assigned to z is 1.0 n = x*y; expression value is 3.15, value assigned to n is 3 Explicit conversion of data types -- casting int n, d; double frac; frac = (double)n / (double)d; Why not frac = (double) n/d; ? Character types Character set: ASCII (American Standard Code for Information Interchange) A collating sequence is a sequence of characters arranged by character code numbers, program: col_seq.c compare character using ==, !=, <, <=, >, >= (it is just the comparson the values of characters) 'A' < 'B' < .... e.g. char ch; int i; i = 'a'; /* i is now 97 */ ch = 65; /* ch is now 'A' */ ch = ch+1; /* ch is now 'B' */ ch++; /* ch is now 'C' */ Reading and writing characters (a) use scanf and printf char ch; scanf("%c",&ch); /* read a single character */ printf("%c",ch); /* write a single character */ do { scanf("%c",&ch); } while ( ch!= '\n'); (b) use getchar and putchar putchar(ch); ch = getchar(); do { ch = getchar(); } while ( ch != '\n'); while ( (ch = getchar()) !='\n'){ ... } program: length.c, length2.c New type definitions define ``new'' type to make the program more understandable with careful chosen meaningful type name in a particular application e.g. typedef double Dollars; Dollars cash_in, cash_out; vs double cash_in, cash_out; Enumerated types syntax: typedef enum {identifier_list} enum_type; The first identifier is represented by the integer 0, The second by 1, and so on. Using enumerated types makes programs more readable because the type's values are meaningful for a particulr application e.g. typedef enum {entertainment, rent, utilities, food, clothing, automobile, insurance, miscellaneous} expense_t; expense_t expense_kind; see code: expenses.c e.g. typedef enum {monday, tuesday, wednesday, thursday, friday, saturday, sunday} day_t; day_t today; see code: week_hours.c Function parameters question: how to include a function in the parameter list of another function? why: application in bisection method to find a root of an equation f(x) = 0; answer: simply including a prototype of the function in the parameter list; example: eval_fun.c bisection.c