#include <stdio.h>
int x = 77; /* global variable x*/
void Fn1 ()
{
extern int y; /* Use external global variable y, if no extern compilation does not pass */
printf ("Fn1 (): x=%d,y=%d\n", x, y); x=77,y=88;
}
void Fn2 ()
{
extern int y; /* Use external global variable y, if no extern compilation does not pass */
y=888; /* Modify the external global variable Y to 888*/
printf ("Fn2 (): x=%d,y=%d\n", x, y); x=77,y=888;
}
int y = 88; /* global variable y*/
void Fn3 ()
{
printf ("Fn3 (): x=%d,y=%d\n", x, y); X=77,y=888,y does not need to use extern because Y is defined in front of FN3 ().
}
int main ()
{
int x = 10; Local variables within the/*main method x*/
if (x>0)
{
int x = 100; /*if local variables within a statement x*/
x/=2;
printf ("If statement inside, x=%d\n", X); x=50;
}
printf ("Main method inside, x=%d\n", x);//x=10;
FN1 ();
FN2 ();
Fn3 ();
Getch ();
return 0;
}
Operation Result:
------------------------------------------
Within the IF statement, x=50
Within the main method, x=10
FN1 (): x=77,y=88
FN2 (): x=77,y=888
Fn3 (): x=77,y=888
This article is from the Java Technology Alliance blog, so be sure to keep this source http://simoniu.blog.51cto.com/2566344/1591746
An example of C-language local variables and global variables