Use of c global variables and local variables (when variable names are the same)

Source: Internet
Author: User

In c language, variables have global variables and local variables , which are similar to many high-level languages such as C#,java. However, the local variables in the C#,java are not allowed to be the same as global variable names in the scope of global variables, and the C language is allowed to do so. This is highly discouraged, but if C is allowed to do so, analyze the value of the variable whose variable name is actually called when the variable name is called at different locations.

The code is as follows:

#include <stdio.h>//Global Variables//scope: Start from definition to end of file//default initial value is 0int c = 1;intTest () {int c = 2; //this position is int c = 1; int c = 2; all works .//The last C = 2 of the scope is used;//2    printf ("%d\n" , c);}//When there are more than one variable with the same name when calling a variable//The variable that uses the scope closest to itself//133423intMain () {//this position only you int c = 1;//1    printf ("%d\n", c); //Local Variables//Scope : From definition start to function end//no default initial value   int c = 3; //this position is int c = 1; int c = 3; all works .//The last C = 3 of the scope is used;//3    printf ("%d\n" , c); {        //this position is int c = 1; int c = 3; all works .//The last C = 3 of the scope is used;//3        printf ("%d\n", c); //Local Variables//scope: Start from definition to end of code block//no default initial value        int c = 4; //this position int c = 1; int c = 3;int c = 4; all works .//The last C = 4 of the scope is used;//4        printf ("%d\n" , c); }        //view inside the test () function//2test (); //this position is int c = 1; int c = 3; all works .//The last C = 3 of the scope is used;//3    printf ("%d\n", c); return 0;}

Results:

Summary: When more than one variable with the same name is in effect when calling a variable, use the scope closest to the variable

Use of c global variables and local variables (when variable names are the same)

Related Article

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.