Scope of variables in C/C ++: local variables, global variables, and file-level variables

Source: Internet
Author: User

Scope of variables in C/C ++: local variables, global variables, and file-level variables

Variables in C/C ++ are classified into global variables and local variables. This division method is based on variables.Visible RangeOrScope.

1. Local variable

Local variables are defined in{}And its scope is also within this range. Although common local variables are defined in the function body, we can also manually add a pair of braces to limit the scope of variables.

As follows:

void f(){    float x = 0;    {        int a;    }}

Don't underestimate the scope. This has a much greater impact on C ++ than pure C. When a local variable in C language leaves the scope, the compiler inserts a POP command to clear the stack space occupied by the variable. In C ++, apart from the POP command, the Destructor must be called.

Class MyClass {MyClass (){}~ MyClass () {}}; void f () {MyClass a ;}// the C ++ compiler is inserted and called here ~ MyClass () code do_someting ();}

The C ++ compiler automatically inserts a call before the end of the scope of object ~ Assembly Code of MyClass.

The local variable scope is enforced by the compiler. In this way, an error will be reported during compilation once an out-of-scope access occurs, so as to help programmers eliminate errors.

2. Global Variables

The scope of global variables is the whole project, that is, they are visible in all the files involved in the link. This leads to a problem-Name Conflict. For example, the following project contains three source files main. c, 1.c, and 2.c.

Main. c

#include 
  
   int main(int argc, char** argv){    return 0;}
  

1. c

int a = 1;

2. c

int a = 2;

Every file can be compiled, but an error will be reported during the link, because 1. c and 2. c use the global variables with the same name. Therefore, the global variables in C language are very bad. The dogma that does not even use global variables is prevalent in a wide range.

However, global variables are often necessary, at least using them will make the problem easier. For example, when a variable is a parameter of many functions.

void f1(int a);void f2(int a);void f3(int a);

In this way, the variable a needs to be passed every time a function is called. When the number of such parameters increases, it will make people crazy. For example

void f1(int a, int b, int c, int d, int e);void f2(int a, int b, int c, float g);void f3(int a, int b, int c, int d);

This situation is common in programs that need to save the state, such as the GDI library and OpenGL library. It is a good choice to use global variables to maintain state data. C ++ sees this need, so simply bind these State data with Algorithm functions to formClassTo simplify the code design.

3. File-level variables

In many cases, the visible scope of variables required by programmers is not the whole project, nor the function, but visible in the current file. C language providesStatic global variables. Static global variable. This name does not reflect the scope of the variable scope. It is a very bad name. In addition, the keyword static is confusing.

static int a = 100;

The designers of C language may want to save the use of keywords. Many keywords have different meanings in different places. This design should be benevolent. I personally think it would be easier to understand if other keywords such as internal are used here.

internal int a = 100;

It seems that a similar keyword exists in C # to indicate the scope.

To put it bluntly, the static global variable is only valid within the file defining it, and cannot be referenced in other files. The preceding example is changed:
Main. c

#include 
  
   int main(int argc, char** argv){    return 0;}
  

1. c

static int a = 1;

2. c

int a = 2;

The project is linked successfully. Because there is only one global variable named a with a value of 2 in the global range, the with a value of 1 is valid only in 1. c.

4 C and C ++ compilers have different const constants.

The C ++ compiler automatically adds the static keyword to the const constant so that its scope is at the file level. C language compiler does not. The following code:

Main. c

#include 
  
   int main(int argc, char** argv){    return 0;}
  

1. c

const int a = 1;

2. c

int a = 2;

The C ++ compiler can successfully compile the link, but an error is returned when the C compiler is used. For code portability, it is best to manually write all static const.

Main. c

#include 
  
   int main(int argc, char** argv){    return 0;}
  

1. c

static const int a = 1;

2. c

int a = 2;

The above code can be compiled successfully in the C and C ++ compilers.

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.