[Original] C ++ variable scope (2)

Source: Internet
Author: User

In the scope of C ++ variables (1), the static variables defined by static variables are added that static variables are always in the static storage zone from the beginning to the end of the program, the address is fixed, so any operation on it will be retained. Even if a variable is declared and defined in a function, it will still exist in a static zone for a long time and cannot be used by other functions or other places, static variables are classified as static variables, but there are still local and global variables.

In C ++ variable scope (1), it is also said that adding the const modifier before the variable indicates that the variable can only be valid in the CPP, but can the variables defined by const be accessed in other files? The answer is yes, but the format must be shown in the following Demo, for example:


1/** // file1.cpp ////// //////////////////////////////////
2
3 Extern const int a = 1;
4
5/** // file. cpp /////////////////////////////////////// ///
6
7 Include <iostream>
8
9 Using namespace std;
10
11 Extern const int;
12
13 Cout <a <endl; // print 1

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.