《C语言编程学习课件 (39).pdf》由会员分享,可在线阅读,更多相关《C语言编程学习课件 (39).pdf(8页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Programming In CProgramming In C Programming In CProgramming In C Properties of variables The application of functions makes the C program realize the modular structure and the mutual call between functions.In addition,the characteristics such as the scope and lifetime of variables are also highligh
2、ted.int x=3;This statement embodies three basic attributes of variables:variable data type,variable name and value stored in the variable.Data type name value According to different scopes,variables in C can also be divided into two types:local variables and global variables.Programming In CProgramm
3、ing In C Global and Local Variables Local variables are variables defined within the function body or within compound statements.For example,the formal parameters of a function are local variables.int max(int a,int b)int a;a=ab?a:b;return a;void main()int a,b;printf(请输入两个整数:);scanf(%d%d,&a,&b);print
4、f(%d is maxinumn,max(a,b);Example of local variable Local variables cannot have the same name in the same scope.Error message error C2082:redefinition of formal parameter a The scope of local variablea,b in function body Formal parameter a and local variable a has the same name and scope.It is canno
5、t accept by C language.Programming In CProgramming In C#include stdio.h void main()int counter=0,i,mes;for(i=1;i=10;i+)int counter=0,k;for(k=1;kb?a:b;return(c);main()int a=8;printf(max=%d,max(a,b);code of global and local variables Analysis of program The program is executed from the main()function
6、entry.When the max()function is called,the argument a is the local variable which inside the main()function body,and the argument b is undefined in the main()function,so it is from a global variable with 5.In the max()function,the values obtained by the formal parameters correspond to the actual par
7、ameters respectively,that is,8 and 5 are compared,and the larger one is returned to the main()function and output.Programming In CProgramming In C The important points in this section:What is the scope of a variable?Where should global variables and local variables be defined?What does the compiler do when the scopes of global variables and local variables overlap?Programming In CProgramming In C