Variable data object declaration in C two methods

Variable (data object) declaration in C two methods of variable declaration Value-oriented int address name type Address-oriented a; 0065 FDF 4 a int 154 int address name type *pa; 0065 FE 4 D pa int address 0065 FDF 4 a ==> value (154) pa ==> address (0065 FDF 4) &pa ==> address (0065 FE 4 D) Engr 0012 (04 -1) Lec. Notes 20 -01 *pa ==> value (154)

arithmetic and logical operators in C integer division whenever values on both sides of operator are integers otherwise, if one is floating point, promotion takes place Engr 0012 (04 -1) Lec. Notes 20 -02

integer division whenever values on both sides of operator are integers a = 38/3; (= 12) b = 38%3; (= 2) long division is performed Engr 0012 (04 -1) Lec. Notes 20 -03

promotion if values are of different type in an expression a = 38. 0/3; subset type will be promoted to superset type before executing ==> a = 38. 0/3. 0; (==> 12. 66666…) can safely assign values of subset type to superset type // variable declaration int a = 3; double b; … // algorithm b = a; Engr 0012 (04 -1) Lec. Notes 20 -04 promoting type int value to type double

demotion // variable declaration int a; double b = 3. 2; … // algorithm a = b; demoting type double value to type int don’t do it - may lead to unexpected results Engr 0012 (04 -1) Lec. Notes 20 -05

trace main() { // begin main // variable declaration int a = 3, b = 3, c = 2, d = 7, e, f; double alfa = 2. 0, beta = 5. 0, gamma = 4. 0, delta, epsilon; // algorithm delta = (a*b/c)*gamma; e = d%b; epsilon = (alfa*beta/gamma)*b; beta = (1/2)*beta; f = d/b; printf( "ndelta = %10. 2 f ne = %d nepsilon = %. 3 f" "nbeta = %f nf = %8 d nn", delta, e, epsilon, beta, f); } // end main Engr 0012 (04 -1) Lec. Notes 20 -06

functions prototype double void int void name( parameter list ); mycos( double angle ); convert( double bearing, char *pface, double *pturn, char *pturndir ); getint 1( void ); getint 2( int *pvalue ); type of value sent back by return statement values required, addresses shared Engr 0012 (04 -1) Lec. Notes 20 -07

functions double mycos( double angle ); returns type double value through return statement void requires type double value to do its job convert( double bearing, char *pface, double *pturn, char *pturndir ); returns no values through return statement Engr 0012 (04 -1) Lec. Notes 20 -08 requires one type double value to do its job and shares three addresses with calling function

functions int getint 1( void ); returns type int value through return statement void requires no values to do its job getint 2( int *pvalue ); returns no values through return statement shares one address with calling function Sharing an address means that both the function called and the calling function have access to the information at that address (and can change the information at the address) Engr 0012 (04 -1) Lec. Notes 20 -09

functions int */ getint 1( void ) function purpose function needs function results agreement between */ { // begin getint 1 value-oriented declaration // variable declaration and address provided int value; in scanf // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, &value ): return( value ); } // end getint 1 agreement between declared type and placeholder Engr 0012 (04 -1) Lec. Notes 20 -10

functions void getint 2( int *pvalue ) */ function purpose agreement between function needs address-oriented declaration function results and address provided */ { // begin getint 2 in scanf // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, pvalue ): } // end getint 2 agreement between declared type and placeholder Engr 0012 (04 -1) Lec. Notes 20 -11

functions can be called from any other functions can even call themselves (recursion) rules on calling statements must have same number of parameters as prototype each parameter must be of same type value parameters require values can be supplied in at least five ways values could be promotable but not demotable address parameters require addresses Engr 0012 (04 -1) Lec. Notes 20 -12

functions rules on calling statements, continued variable type is never used in calling statement a void parameter list is denoted by empty parentheses values sent back by the return statement may be captured with an assignment statement (otherwise, return statement values are lost) Engr 0012 (04 -1) Lec. Notes 20 -13

functions prototype call examples double mycos( double angle ); accel = mycos( 3. 2 ); potential = mycos( 4. 0*PI*current/3. 0 ); position = mycos( 2 ); force = mycos( x ); velocity = mycos( *ptime ) intensity = mycos( sin(x) ); values can be provided by: a function call Engr 0012 (04 -1) Lec. Notes 20 -14 providing an actual value an arithmetic expression an (proper) variable name

functions examples prototype void call convert( 333, &face, &turn, &direction ); prototype int call Engr 0012 (04 -1) Lec. Notes 20 -15 getint 1( void ); choice = getint 1( ); prototype void call convert( double bearing, char *pface, double *pturn, char *pturndir ); getint 2( int *pvalue ); getint 2( &choice );

trace int trace( int v 1, int *pv 2); main() { // begin main // variable declaration int a, b, c; // algorithm a = trace(1, &b, &c); printf( "na = %d nb = %d nc = %d" a, b, c); } // end main //******************* int trace(int v 1, int *pv 2, int *pv 3) { // begin trace // algorithm printf(“Enter integer ==> ”); scanf( “%d”, pv 2); *pv 3 = v 1*(*pv 2); return(v 1 + (*pv 2) + (*pv 3) ); } // end trace Engr 0012 (04 -1) Lec. Notes 20 -16
- Slides: 16