Notes for Lecture 6 - February 1, 2007 ====================================== * Type double revisited ** Representation *** 3 fields **** sign bit **** exponent (11 bits) **** significant (53 bits, including hidden leading 1 bit) *** See http://en.wikipedia.org/wiki/IEEE_754 for further information ** float.h contains several constants describing range and limits of doubles See demo 06.1_dbl for further information ** Doubles can be converted to ints. *** Meaning: drop fractional part (i.e., truncate towards zero) *** Examples: -2.71 ==> -2, 3.4 ==> 3. *** Explicit cast: (int)e, where e is an expression of type double *** Implicit via assignment: k=e; where k is int and e is double. *** Implicit from function: return e; where e is double and function returns int. See demo 06.1_dbl. * Command line arguments ** String arguments passed by shell from command line to the program ** Example: mycommand foo 123 has two string arguments: "foo" and "123". ** Main program must have the prototype: int main( int argc, char* argv[] ) *** argc: number of strings in argv[] *** argv: strings passed in argv[0]...argv[argc-1]. *** argv[0]: leaf name or full path name used to invoke the command *** argv[1]: first argument following command name *** argv[2]: second argument following command name, etc. ** Example: mycommand foo 123 would result in argc==3 and argv[0]...argv[2] containing three strings: "mycommand", "foo", and "123". See demo 06.2_cmdline * Files and streams ** Definitions: A file is a sequence of bytes stored on disk. An input stream is a source of bytes from a file, keyboard, or other device An output stream is a sink of bytes, going to a file, screen, or other device ** Three streams are open by default: *** stdin (standard input) *** stdout (standard output) *** stderr (standard error) [we haven't discussed this yet] ** fopen() creates a new stream from/to a file ** Return type of fopen() is FILE*. (FILE is defined in .) ** fopen( filename, "r" ) *** filename is string giving name of a file *** "r" specifies to open the file for reading *** fopen returns NULL if it fails to open the file *** fopen creates a new input stream if successful and returns a pointer to it. ** reading from a stream *** getchar() and scanf() read from stdin *** getc(), fgetc(), and fscanf() take an extra stream argument and read from it See demo 06.3_files * Returning multiple values from a function ** Can return results through pointer arguments (like scanf() does) ** Example: to return two doubles val1 and val2: *** Function prototype: myfun( double* xp, double* yp ); *** Function call: myfun( &x, &y ), where x and y are variables of type double *** Within definition of myfun(): *xp = val1; *yp = val2. ** Actual meaning of & and * will be described later. See demo 06.4_ptrargs