130 likes | 234 Views
Engr 0012 (04-1) LecNotes 19-01. address (where located in computer). type of information. value. C assigns this. programmer assigns this. user or programmer assigns this. Variable (data object) declaration in C. variable attributes. Engr 0012 (04-1) LecNotes 19-02.
E N D
address (where located in computer) type of information value C assigns this programmer assigns this user or programmer assigns this Variable (data object) declaration in C variable attributes Engr 0012 (04-1) LecNotes 19-02
Variable (data object) declaration in C types of variables Engr 0012 (04-1) LecNotes 19-03
Value-oriented Address-oriented int a; int *pa; 0065FDF4 0065FE4D a 154 pa 0065FDF4 int int address Variable (data object) declaration in C two methods of variable declaration address address name name type type a ==> value (154) pa ==> address (0065FDF4) &pa ==> address (0065FE4D) &a ==> address (0065FDF4) *pa ==> value (154) Engr 0012 (04-1) LecNotes 19-04
seen in prototype parameter list seen in variable declaration section Variable (data object) declaration in C value-oriented declarations int myfunction( int i1, double d1 ) // myfunction description { //begin myfunction // variable declaration int i2,i3; // two ints double d2,d3; // two doubles char c1; // a character // algorithm … } //end myfunction most common declaration - emphasizes value Engr 0012 (04-1) LecNotes 19-05
sets up memory map on scrap paper &i1 &d1 &i2 &i3 &d2 &d3 &c1_ 43.2 ?? ?? ?? ?? ?? where did these values come from? Variable (data object) declaration in C value-oriented declarations int myfunction( int i1, double d1 ) // myfunction description { //begin myfunction // variable declaration int i2,i3; // two ints double d2,d3; // two doubles char c1; // a character // algorithm … } //end myfunction Engr 0012 (04-1) LecNotes 19-06
seen in prototype parameter list Variable (data object) declaration in C address-oriented declarations void myfnc2( int i1, int *pi2, int *pi3 ) // myfnc2 description { //begin myfunction // variable declaration int i4,i5; // two ints double d1,d2; // two doubles char c1; // a character // algorithm … } //end myfunction used when we want to share an address between functions Engr 0012 (04-1) LecNotes 19-07
contents are addresses! sets up memory map on scrap paper &i1 &pi2 &pi3 &i4 &d1 &c1_ 462AE62AF ?? ?? ?? where do these come from? Variable (data object) declaration in C address-oriented declarations void myfnc2( int i1, int *pi2, int *pi3 ) // myfnc2 description { //begin myfunction // variable declaration int i4; // an int double d1; // a double char c1; // a character // algorithm … } //end myfunction Engr 0012 (04-1) LecNotes 19-08
function prototypes/calling statements value-oriented parameters require values address-oriented parameters require addresses double fcn1( int i1, double d1, int *pi2 ) possible calling statements a = fcn1( 1, 3.2, &myint ); q = fcn1( alpha, beta, &thisint ); w = fcn1( alpha, cos(beta), &anyint ); Engr 0012 (04-1) LecNotes 19-09
getting information from keyboard: scanf scanf requires two things type of value expected - denoted by placeholder where to store value - address of variable printf( “\nPlease enter an integer ==> ”); scanf( “%d”, &i1 ); need to printf before scanf printf( “\nPlease enter a real number ==> ”); scanf( “%lf”, &d1 ); caution - always flush keyboard buffer before & after char input printf( “\nPlease enter a character ==> ”); fflush( stdin ); scanf( “%c”, &c1 ); fflush( stdin ); Engr 0012 (04-1) LecNotes 19-10
displaying information to screen: printf printf requires a control string that tells what to display values to display are optional!!! printf( “\nPlease enter an integer ==> ”); to display values, insert placeholders in control string & list values (in order) after control string printf( “\nCircle of radius %.4lf has area %.4lf”, 1, 3.14159 ); printf( “\nCircle of radius %.4lf has area %.4lf”, radius, circ_area ); Engr 0012 (04-1) LecNotes 19-11
scanf simply enclose placeholder “%d” in double quotes printf can format output display w is a number that tells how many spaces to use %wd w tells how many (total) spaces to use %w.plf p tells how many decimal places to use le: (long) scientific notation display %w.ple placeholders Engr 0012 (04-1) LecNotes 19-12
is the same as a=6.3; is the same as a = 6.3; no comma! general notes on C C ignores whitespace - semicolons terminate statements a = 6.3; printf( “\nCircle of radius %.4lf has area %.4lf”, radius, circ_area ); to break a control string for easy reading in the text editor printf( “\nCircle of radius ” “%.4lf has area %.4lf”, radius, circ_area ); Engr 0012 (04-1) LecNotes 19-13