C/Language Basics/Variable Scope
Содержание
- 1 A free-standing block statement
- 2 Assign value to global variable
- 3 Define and use global variable and function
- 4 Define and use Global variables
- 5 Define block inside a function
- 6 Define local variable for a function
- 7 Define variable in a block
- 8 Global variable and function
- 9 Our own power function with parameters
- 10 Use global variable in function
- 11 use global vars
- 12 Variable scope: in and out a block
- 13 Variable scope:inside a function
- 14 Work with global variable
A free-standing block statement
<source lang="cpp">
- include <stdio.h>
int main(void) {
int i; { i = 1; printf("%d", i); } return 0;
}
</source>
Assign value to global variable
<source lang="cpp">
- include <stdio.h>
int count; int main(void) {
extern int count; /* this refers to global count */ count = 10; printf("%d", count); return 0;
}
</source>
Define and use global variable and function
<source lang="cpp">
- include <stdio.h>
int i =0; //Global variable void f1(void) {
int k; // local variable for f1. i = 50;
}
main() {
int j; // local variable in main f1(); i =0; printf(" i in main %d\n",i); f1(); printf(" i after call%d\n",i);
}
</source>
Define and use Global variables
<source lang="cpp">
- include <stdio.h>
int count = 0; /* Declare a global variable */ /* Function prototypes */ void test1(void); void test2(void); void main() {
int count = 0; /* This hides the global count */ for( ; count < 5; count++) { test1(); test2(); }
} /* Function test1 using the global variable */ void test1(void) {
printf("\ntest1 count = %d ", ++count);
} /* Function test2 using a static variable variable */ void test2(void) {
static int count; /* This hides the global count */ printf("\ntest2 count = %d ", ++count);
}
</source>
Define block inside a function
<source lang="cpp">
- include <stdio.h>
main() {
int i = 10; { int i = 0; for( i=0;i<2;i++) { printf("i = %d\n",i); } } printf("i = %d\n",i);
}
</source>
Define local variable for a function
<source lang="cpp"> int i =0; //Global variable void f(void); main() {
int i ; // local variable for main i =0; printf("value of i in main %d\n",i); f(); printf("value of i after call%d\n",i);
} void f(void) {
int i=0; //local variable for f i = 50;
}
</source>
Define variable in a block
<source lang="cpp">
- include <stdio.h>
main() {
int k = 10; int i = 100; { int i; for(i = 0; i < 2; i++ ) { printf("i = %d\n",i); } } printf("i = %d\n",i);
}
</source>
Global variable and function
<source lang="cpp">
- include <stdio.h>
int count; /* count is global */ void func1(void); void func2(void); int main(void) {
count = 100; func1(); return 0;
} void func1(void) {
int temp; temp = count; func2(); printf("count is %d", count); /* will print 100 */
} void func2(void) {
int count; for(count=1; count<10; count++) putchar(".");
}
</source>
Our own power function with parameters
<source lang="cpp">
- include <stdio.h>
int power(int m, int e); int main(void) {
int m, e; m = 3; e = 4; printf("%d to the %d is %d\n", m, e, power(m, e)); printf("5 to the 6th is %d\n", power(5, 6)); printf("4 to the 4rd is %d\n", power(4, 4)); return 0;
} /* Parameterized version of power. */ int power(int m, int e) {
int temp; temp = 1; for( ; e > 0; e--) temp = temp * m; return temp;
}
</source>
Use global variable in function
<source lang="cpp">
- include <stdio.h>
int max; /* a global variable */ void f(void) {
int i; for(i=0; i<max; i++) printf("%d ", i);
}
int main(void) {
max = 10; f(); return 0;
}
</source>
use global vars
<source lang="cpp">
- include <stdio.h>
/* global definition of first and last */ int first = 10, last = 20; int main(void) {
extern int first, last; printf("%d %d", first, last); return 0;
}
</source>
Variable scope: in and out a block
<source lang="cpp">
- include <stdio.h>
void main() {
int c = 0; /* Declared in outer block */ do { int c = 0; /* This is another variable called c */ ++c ; /* this applies to inner c */ printf("\n c = %d ", c ); } while( ++c <= 3 ); /* This works with outer c */ /* Inner c is dead, this is outer */ printf("\n c = %d\n", c );
}
</source>
Variable scope:inside a function
<source lang="cpp">
- include <stdio.h>
int main(void) {
int x; x = 10; if(x == 10) { int x; /* this x hides the outer x */ x = 99; printf("Inner x: %d\n", x); } printf("Outer x: %d\n", x); return 0;
}
</source>
Work with global variable
<source lang="cpp">
- include <stdio.h>
int sample; int main(void) {
printf("%p", &sample); return 0;
}
</source>