C + + scope and identifier visibility __c++

Source: Internet
Author: User
Tags function prototype

Scope refers to the range in which an identifier can be used. Only identifiers within the scope can be accessed (called "visible"). This section only discusses local domain and file domains (global domains), where local fields include block fields and function prototype domains.

The starting point for any identifier scope is the identifier description.
Block Scope

"Block" refers to a piece of code enclosed by a pair of braces. The identifier defined in the block, scoped within the block. A compound statement is a block. The identifier defined in the compound statement, and the scope is only within that compound statement. A function is also a block. The identifiers defined in the function, including the parameters and local variables defined in the function body, are within that function, also known as "function fields."

Example: "Example 3.7" input two number, the two number in order from large to small to save, and output the results. (View animation demo)
#include <iostream>
using namespace Std;
int main () {
int a,b; Defines local variables within a function, with a function field
cout<< "Enter two integers:" <<endl;
cin>>a>>b;
cout<< "a=" <<a<< '/t ' << "b=" <<b<<endl;
if (b>=a) {//Keep a large number in a, B save decimal
int t; Block to define a local variable with a block scope
T=a; A=b;                b=t; Exchange A,b values
}
cout<< "a=" <<a<< '/t ' << "b=" <<b<<endl;
return 0;
}

Example: "Example 3.9" shows an example of the visibility of a variable with the same name. (View animation demo)
#include <iostream>
using namespace Std;
int n=100;
int main () {
int i=200,j=300;
cout<< n<< '/t ' <<i<< '/t ' <<j<<endl; {//Output global variable N and outer local variable I,j
int i=500,j=600,n; Inner Layer Block
N=i+j;
cout<< n<< '/t ' <<i<< '/t ' <<j<< Endl; Output inner-layer local variables N and i,j
cout<<::n<<endl; Output global variable N
}
N=i+j; modifying global variables
cout<< n<< '/t ' <<i<< '/t ' <<j<< Endl; Output global variable N and outer local variable I,j
return 0;
}
function Prototype Scope

Function prototypes are not defined functions. When you declare a function prototype, the formal parameter scope is only in the prototype declaration, that is, the scope ends with the closing parenthesis. Precisely because formal parameters cannot be referenced elsewhere in the program, formal parameter names can be omitted as long as the number and type of parameters are declared.
#include <iostream.h>
void swap (Int,int);//function prototype scope
void Main () {
......
}
File Scopes

The file scope is also called global scope. An identifier that is defined outside all functions, has a file scope, and the scope ends from the definition to the entire source file. Global variables and functions defined in the file have a file scope. If a file describes an identifier with a file scope, and the file is contained by another file, the scope of the identifier extends to the new file. For example, CIN and cout are identifiers with file scopes that are described in header file iostream.h, and their scope extends to files embedded in iostream.h.

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.