EE31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING
C Programming review PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu C Programming Review
Program Structure local and global variable Storage classes Pointers fUnction Call PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Program Structure •Local and Global Variable •Storage Classes •Pointers •Function Call
Local variable variables declared within a function body include int main o int next, n; / next input, number of inputs * long 0; / running total * double avgi /* average of input values * for (n=0; scanf("i",&next)== 1;n++) sum += nexti avg =(n== 0)? 0.0:(double)sum/n; printf("Average of %i values is f.\n",n, avg)i return 0; next, sum, avg are local variables of main PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Local Variable • variables declared within a function body #include int main() { int next, n; /* next input, number of inputs */ long sum = 0; /* running total */ double avg; /* average of input values */ for (n = 0; scanf("%i", &next) == 1; n++) sum += next; avg = (n == 0) ? 0.0 : (double) sum / n; printf("Average of %i values is %f.\n", n, avg); return 0; } next, sum, avg are local variables of main
Local variable initializing local variables don'tcount on local variables automatically being initialized to zero long sum =0;/* running total * PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Local Variable • initializing local variables • don’t count on local variables automatically being initialized to zero long sum = 0; /* running total */
Local variable variables declared within a block include int main o int next, n;/* next input, number of inputs * long SUnn i/* running total * for (n =0; scanf(gi,&next)== 1; n++)i sum+=nex七 /* compute and print input average * double avg =(n == 0)? 0.0: (double)sum /n; 2 printf("Average of i values is f.\n", n, avg)i return 0; avg is declared when it is needed PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Local Variable • variables declared within a block avg is declared when it is needed. #include int main() { int next, n; /* next input, number of inputs */ long sum = 0; /* running total */ for (n = 0; scanf("%i", &next) == 1; n++){ sum += next; { /* compute and print input average */ double avg = (n == 0) ? 0.0 : (double) sum / n; printf("Average of %i values is %f.\n", n, avg); } return 0; }
Local variable block/compound statements group of statements surrounded by braces statement statement Or statement statement PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Local Variable • block/compound statements group of statements surrounded by braces { statement; . . statement; } or statement, statement;
Scope of variable int maino int sq(int x) intx: XXX, return x: X=25 x=sq x) printf((%dln”,x); return o 港城市大 PROGRAMMING METHDOLOGY AND SOFTWAREENGINEERING Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu int main() { int x; x= 25; x= sq (x); printf(“%d\n”, x); return 0; } int sq ( int x) { x=x*x; return x; } Scope of Variable
Scope of variable include int sa (int x)i int main fint xi 25 printf(" The address of x in main is d\n",&x)i x= sq(x)i printf("gd\n", x) return 0 int sq(int a) printf(" The address of a in sq is d \n", &a) printf(" what is x d\n", x)i a=aai return ai 港城市大 PROGRAMMING METHDOLOGY AND SOFTWAREENGINEERING Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Scope of Variable #include int sq(int x); int main() {int x; x= 25; printf(" The address of x in main is %d\n", &x); x= sq(x); printf("%d\n", x); return 0; } int sq(int a) { printf(" The address of a in sq is %d\n", &a); printf(" what is x %d\n", x); a=a*a; return a; }
Local variable Lifetime they exist only from block entry to block exit they exist/are accessible only within the function where they are declared when enter a block/function, space is reserved for its local variables when exit the block/function the reserved space Is free PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Local Variable Lifetime • they exist only from block entry to block exit • they exist/are accessible only within the function where they are declared • when enter a block/function, space is reserved for its local variables • when exit the block/function, the reserved space is free