Download presentation
Presentation is loading. Please wait.
1
Engr 0012 (04-1) LecNotes 19-01
2
Variable (data object) declaration in C
variable attributes address (where located in computer) C assigns this type of information programmer assigns this value user or programmer assigns this Engr 0012 (04-1) LecNotes 19-02
3
Variable (data object) declaration in C
types of variables Engr 0012 (04-1) LecNotes 19-03
4
154 Variable (data object) declaration in C 0065FDF4
two methods of variable declaration Value-oriented int a; Address-oriented int *pa; address 154 a int 0065FDF4 address 0065FDF4 pa int address 0065FE4D 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
5
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 seen in prototype parameter list seen in variable declaration section most common declaration - emphasizes value Engr 0012 (04-1) LecNotes 19-05
6
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 sets up memory map on scrap paper &i1 &d1 &i2 &i3 &d2 &d3 &c1_ ?? ?? ?? ?? ?? where did these values come from? Engr 0012 (04-1) LecNotes 19-06
7
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 seen in prototype parameter list used when we want to share an address between functions Engr 0012 (04-1) LecNotes 19-07
8
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 sets up memory map on scrap paper &i1 &pi2 &pi3 &i4 &d1 &c1_ 4 62AE 62AF ?? ?? ?? contents are addresses! where do these come from? Engr 0012 (04-1) LecNotes 19-08
9
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
10
getting information from keyboard: scanf
scanf requires two things type of value expected - denoted by placeholder where to store value - address of variable need to printf before scanf printf( “\nPlease enter an integer ==> ”); scanf( “%d”, &i1 ); 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 ); Engr 0012 (04-1) LecNotes 19-10
11
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, ); printf( “\nCircle of radius %.4lf has area %.4lf”, radius, circ_area ); Engr 0012 (04-1) LecNotes 19-11
12
placeholders scanf simply enclose placeholder “%d” in double quotes
printf can format output display %wd w is a number that tells how many spaces to use %w.plf w tells how many (total) spaces to use p tells how many decimal places to use %w.ple le: (long) scientific notation display Engr 0012 (04-1) LecNotes 19-12
13
general notes on C C ignores whitespace - semicolons terminate statements a = 6.3; is the same as a=6.3; is the same as 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 no comma! printf( “\nCircle of radius ” “%.4lf has area %.4lf”, radius, circ_area ); Engr 0012 (04-1) LecNotes 19-13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.