当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING(PPT讲稿)C Programming Review

资源类别:文库,文档格式:PPT,文档页数:50,文件大小:411.5KB,团购合买
点击下载完整版文档(PPT)

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

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共50页,可试读17页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有