local variable
The independence of function is guaranteed.
- Variables defined within a function
- It also allows the definition of local variables in action recombination statements
- Generally defined at the beginning of a function or compound statement
global variable
- Variables defined outside a function and not belonging to any function
- Generally defined at the front of a function
- However, when local variables and global variables are renamed, local variables shall prevail within the function.
Example 5-7 #include<stdio.h> int x; int f(); int main() { int a=1; x=a; //Global variable x a=f(); { int b=2; b=a+b; //Local variable operation x=x+b; //Global Variable Operations } printf("%d %d ",a,x); return 0; } int f() { int x=4; //x is a local variable return x; } 4 7
Example 5-8 #include<stdio.h> float cash; void income(float number),expend(float number); int main() { int choice; float value; cash=0; printf("Enter (0-end,1-income,2-expend):"); scanf("%d",&choice); while(choice!=0){ if(choice==1||choice==2){ printf("Enter cash value:" ); scanf("%f",&value); if(choice==1) income(value); else expend(value); printf("current cash:%.2f\n",cash); } printf("Enter (0-end,1-income,2-expend):"); scanf("%d",&choice); } return 0; } void income(float number) { cash=cash+number; } void expend(float number) { cash=cash-number; } Enter (0-end,1-income,2-expend):1 Enter cash value:1000 current cash:1000.00 Enter (0-end,1-income,2-expend):2 Enter cash value:456 current cash:544.00 Enter (0-end,1-income,2-expend):0
Variable life cycle
Variables are allocated storage units from definition to recovery of storage units at the end of operation
According to this characteristic, local variables are also called automatic variables.
For example: auto int x,y; auto can be omitted.
Memory Distribution of Variable Storage
Dynamic storage is managed using a stack
Static variables
Stored in static storage, its life cycle lasts until the end of the program.
stastic Type Name Variable Table
Example 5-9 #include<stdio.h> double fact_s(int n); int main() { int i,n; printf("Input n: "); scanf("%d",&n); for(i=1; i<=n; i++) printf("%3d!=%.0f\n",i,fact_s(i)); return 0; } double fact_s(int n) { static double f=1; //Define static variables f=f*n; //Call the last value and multiply it by n return (f); } Input n: 6 1!=1 2!=2 3!=6 4!=24 5!=120 6!=720
Exercise 5-2 //Defining f as a global variable can achieve n! #include<stdio.h> int f=1; double fact_s(int n); int main() { int i,n; printf("Input n: "); scanf("%d",&n); for(i=1; i<=n; i++) printf("%3d!=%.0f\n",i,fact_s(i)); return 0; } double fact_s(int n) { f=f*n; return (f); } Input n: 6 1!=1 2!=2 3!=6 4!=24 5!=120 6!=720 //Replacing f with ordinary local variable can't realize n!, because it can't call the last function value like static local variable. #include<stdio.h> double fact_s(int n); int main() { int i,n; printf("Input n: "); scanf("%d",&n); for(i=1; i<=n; i++) printf("%3d!=%.0f\n",i,fact_s(i)); return 0; } double fact_s(int n) { int f=1; f=f*n; return (f); } Input n: 6 1!=1 2!=2 3!=3 4!=4 5!=5 6!=6