An example of C-language local variables and global variables

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.