C Tutorial/Language/Global variables — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 10:32, 25 мая 2010
Declare global variables
A global variable is available to all functions in your program.
A local variable is available only to the function in which it"s created.
- Global variables are declared outside of any function.
- Global variables are typically declared right before the main() function.
- You can also declare a group of global variables at one time:
int s,t;
Define and use Global variables
#include <stdio.h>
int count = 0; /* Declare a global variable */
void test1(void){
printf("\ntest1 count = %d ", ++count);
}
void test2(void){
static int count; /* This hides the global count */
printf("\ntest2 count = %d ", ++count);
}
int main(void){
int count = 0; /* This hides the global count */
for( ; count < 5; count++) {
test1();
test2();
}
return 0;
}
value of i in main 0 value of i after call 50